[25532] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7776 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Feb 13 21:05:32 2005

Date: Sun, 13 Feb 2005 18:05:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 13 Feb 2005     Volume: 10 Number: 7776

Today's topics:
        CGI-Application-3.31: need override=>1 or not? <mjl69mjl69@myaccmyacc.net>
    Re: CGI-Application-3.31: need override=>1 or not? <amead@comcast.net>
    Re: Feeding false an App <hackeras@gmail.com>
    Re: Feeding false an App <nospam@bigpond.com>
    Re: Feeding false an App <hackeras@gmail.com>
        How do I loop through anonymous hash? <noeltd@hotmail.com>
    Re: How do I loop through anonymous hash? (Peter Scott)
    Re: How do I loop through anonymous hash? <noreply@gunnar.cc>
    Re: How do I loop through anonymous hash? <matternc@comcast.net>
    Re: How do I loop through anonymous hash? <noeltd@hotmail.com>
    Re: How to navigate the docs? <jazeker_b_nospamlalala@yahoo.co.uk>
        Mod_perl: can I share a database connection by putting  <nospam@noglory.net>
    Re: naming a variable with the datum from another varia (Anno Siegel)
    Re: Process word occurances in Bible? ioneabu@yahoo.com
    Re: Process word occurances in Bible? ioneabu@yahoo.com
    Re: Process word occurances in Bible? <dave_frank@hotmail.com>
    Re: Process word occurances in Bible? <see.sig@rochester.rr.com>
    Re: Process word occurances in Bible? <see.sig@rochester.rr.com>
    Re: Process word occurances in Bible? (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 13 Feb 2005 20:39:35 GMT
From: mjl69 <mjl69mjl69@myaccmyacc.net>
Subject: CGI-Application-3.31: need override=>1 or not?
Message-Id: <379s87F5a2lgmU1@individual.net>


A slightly altered cut & paste from CGI-Application that works:

sub do_stuff 
{         
        my $self = shift;

        # Get CGI query object
        my $q = $self->query();

        my $output = '';
        $output .= $q->start_html(-title => 'Page One');
        $output .= $q->h1("Hello, This is page one!");
        $output .= $q->start_form();
        $output .= $q->hidden(-name => 'rm', -value => 'mode2', -override => 1);
        $output .= $q->submit();
        $output .= $q->end_form();
        $output .= $q->end_html();

        return $output;
}

Actual sample code from CGI-Application that does not work as expected

   sub showform {
        my $self = shift;

        # Get CGI query object
        my $q = $self->query();

        my $output = '';
        $output .= $q->start_html(-title => 'Widget Search Form');
        $output .= $q->start_form();
        $output .= $q->textfield(-name => 'widgetcode');
        $output .= $q->hidden(-name => 'rm', -value => 'mode2'); #note no override
        $output .= $q->submit();
        $output .= $q->end_form();
        $output .= $q->end_html();

        return $output;
   }

I was wondering why my CGI app was not going to the next page, but it was fixed by adding the override.  I saw that
Sam Tregar did it this way in his book.  Why does  CGI-Application document it without override?  I assumed that as an
extension of CGI, that the author had done away with stickiness internally.

mjl


------------------------------

Date: Sun, 13 Feb 2005 17:54:25 -0600
From: Alan Mead <amead@comcast.net>
Subject: Re: CGI-Application-3.31: need override=>1 or not?
Message-Id: <pan.2005.02.13.23.54.19.266281@comcast.net>

On Sun, 13 Feb 2005 20:39:35 +0000, mjl69 wrote:

> I was wondering why my CGI app was not going to the next page, but it was fixed by adding the override.  I saw that
> Sam Tregar did it this way in his book.  Why does  CGI-Application document it without override?  I assumed that as an
> extension of CGI, that the author had done away with stickiness internally.

I don't know but there is a note in the CGI.pm documentation .. something
like "The behavior of hidden() with respect to stickiness has changed
again"

I find that stickiness is magical in some cases but in cases like yours it
can be annoying.  If you can always remember to override, then fine. 
Otherwise, it's pretty trivial to write your own myhidden() (or mystate())
that ignores stickiness.  Or just emit the HTML (it's only a few
extra characters).

-Alan

-- 
Help out our research and get a free 
personality profile:
http://www.web-data-collection.org



------------------------------

Date: Sun, 13 Feb 2005 20:04:25 +0000 (UTC)
From: Richard Anderson <hackeras@gmail.com>
Subject: Re: Feeding false an App
Message-Id: <Xns95FCE0D11B163hackerasgmailcom@194.177.210.210>

Gregory Toomey <nospam@bigpond.com> wrote in
news:378smrF58lihaU1@individual.net: 

He wont tell me if its done by buil-in c++ function or by calling a dll 
for instance kernel32.dll.

Well i made a prog my self but not a complte one. i dont know how to make 
my program listen to and respinf to cpu id requests. here it is so:

#! /usr/bin/perl

use strict;

print "Content-type: text/plain\n\n";
print <<EOT;
 eax in    eax      ebx      ecx      edx
00000000 00000002 756e6547 6c65746e 49656e69
00000001 00000f29 0602080b 00004400 bfebfbff
00000002 665b5001 00000000 00000000 007b7040
80000000 80000004 00000000 00000000 00000000
80000001 00000000 00000000 00000000 00000000
80000002 20202020 20202020 20202020 20202020
80000003 6e492020 286c6574 58202952 286e6f65
80000004 20294d54 20555043 36302e33 007a4847

Vendor ID: "GenuineIntel"; CPUID level 2

Intel-specific functions:
Version 00000f29:
Type 0 - Original OEM
Family 15 - Pentium 4
Extended family 0
Model 2 -
Stepping 9
Reserved 0

Brand index: 11 [not in table]
Extended brand string: "                  Intel(R) Xeon(TM) CPU 3.06GH
+z"
CLFLUSH instruction cache line size: 8
Initial APIC ID: 6
Hyper threading siblings: 2



------------------------------

Date: Mon, 14 Feb 2005 06:47:31 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Feeding false an App
Message-Id: <379sn3F5bn0dqU1@individual.net>

Richard Anderson wrote:

> Gregory Toomey <nospam@bigpond.com> wrote in
> news:378smrF58lihaU1@individual.net:
> 
> He wont tell me if its done by buil-in c++ function or by calling a dll
> for instance kernel32.dll.
> 
> Well i made a prog my self but not a complte one. i dont know how to make
> my program listen to and respinf to cpu id requests. here it is so:
> 
> #! /usr/bin/perl
> 
> use strict;
> 
> print "Content-type: text/plain\n\n";
> print <<EOT;
>  eax in    eax      ebx      ecx      edx
> 00000000 00000002 756e6547 6c65746e 49656e69
> 00000001 00000f29 0602080b 00004400 bfebfbff
> 00000002 665b5001 00000000 00000000 007b7040
> 80000000 80000004 00000000 00000000 00000000
> 80000001 00000000 00000000 00000000 00000000
> 80000002 20202020 20202020 20202020 20202020
> 80000003 6e492020 286c6574 58202952 286e6f65
> 80000004 20294d54 20555043 36302e33 007a4847
>

Are you a troll or just plain mad?
Sending disassembled machine code as html is just plain bizarre.

If he is running a server thru sockets ask him for the protocol he's
invesnted.

gtoomey


------------------------------

Date: Mon, 14 Feb 2005 01:56:20 +0000 (UTC)
From: Richard Anderson <hackeras@gmail.com>
Subject: Re: Feeding false an App
Message-Id: <Xns95FD2856F8D65hackerasgmailcom@194.177.210.210>

Gregory Toomey <nospam@bigpond.com> wrote in news:379sn3F5bn0dqU1
@individual.net:

Sorry didnt paste the whole code:

#! /usr/bin/perl

use strict;

print "Content-type: text/plain\n\n";
print <<EOT;
 eax in    eax      ebx      ecx      edx
00000000 00000002 756e6547 6c65746e 49656e69
00000001 00000f29 0602080b 00004400 bfebfbff
00000002 665b5001 00000000 00000000 007b7040
80000000 80000004 00000000 00000000 00000000
80000001 00000000 00000000 00000000 00000000
80000002 20202020 20202020 20202020 20202020
80000003 6e492020 286c6574 58202952 286e6f65
80000004 20294d54 20555043 36302e33 007a4847

Vendor ID: "GenuineIntel"; CPUID level 2

Intel-specific functions:
Version 00000f29:
Type 0 - Original OEM
Family 15 - Pentium 4
Extended family 0
Model 2 -
Stepping 9
Reserved 0

Brand index: 11 [not in table]
Extended brand string: "                  Intel(R) Xeon(TM) CPU 3.06GH
+z"
CLFLUSH instruction cache line size: 8
Initial APIC ID: 6
Hyper threading siblings: 2

Also he wont tell me what protocol he uses: but the job is beeing done 
through sockets.

What can i do next?


------------------------------

Date: Sun, 13 Feb 2005 15:08:40 GMT
From: "Atlantis" <noeltd@hotmail.com>
Subject: How do I loop through anonymous hash?
Message-Id: <Y3KPd.382$Ru4.273@newsfe2-win.ntli.net>

Hi, I should know this, but can't seem to get it working...

Basically I've got an anonymous hash that I want to loop through, I've tried
as below (and a couple of other thing) but get various errors (this one give
me ~syntax error at hash.pl line, 7, near ");"

Code Start.
#!c:\perl\bin\perl
use strict;
use warnings;

my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};

foreach my $Key (keys ($hash));
{

 print "$Key == ", $hash->{$Key}, "\n";
}
Code End

Anyone help me with this?

Thank you in advance:)




------------------------------

Date: Sun, 13 Feb 2005 15:17:32 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: How do I loop through anonymous hash?
Message-Id: <gcKPd.376903$8l.257239@pd7tw1no>

In article <Y3KPd.382$Ru4.273@newsfe2-win.ntli.net>,
 "Atlantis" <noeltd@hotmail.com> writes:
>#!c:\perl\bin\perl
>use strict;
>use warnings;
>
>my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
>
>foreach my $Key (keys ($hash));

keys() needs a hash as argument.  You gave it a reference
to a hash instead.  So, dereference $hash to get the hash:

foreach my $Key (keys %$hash)

and get rid of the semicolon.

BTW, it looks weird for a loop variable to be capitalized.

>{
>
> print "$Key == ", $hash->{$Key}, "\n";
>}

-- 
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/


------------------------------

Date: Sun, 13 Feb 2005 16:01:45 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do I loop through anonymous hash?
Message-Id: <3799d1F5aiqkfU1@individual.net>

Atlantis wrote:
> 
> my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
> 
> foreach my $Key (keys ($hash));

You need to dereference the hash reference to grab the keys, and remove 
that statement separator.

     foreach my $Key (keys %$hash)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Sun, 13 Feb 2005 13:58:16 -0500
From: Chris Mattern <matternc@comcast.net>
Subject: Re: How do I loop through anonymous hash?
Message-Id: <392dnabCt5bXPpLfRVn-qw@comcast.com>

Atlantis wrote:

> Hi, I should know this, but can't seem to get it working...
> 
> Basically I've got an anonymous hash that I want to loop through, I've
> tried as below (and a couple of other thing) but get various errors (this
> one give me ~syntax error at hash.pl line, 7, near ");"
> 
> Code Start.
> #!c:\perl\bin\perl
> use strict;
> use warnings;
> 
> my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
> 
> foreach my $Key (keys ($hash));

You haven't dereferenced your reference.  Also the semicolon
doesn't belong here.

foreach my $Key (keys (%$hash))

> {
> 
>  print "$Key == ", $hash->{$Key}, "\n";
> }
> Code End
> 
> Anyone help me with this?
> 
> Thank you in advance:)

-- 
             Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"


------------------------------

Date: Mon, 14 Feb 2005 00:47:20 GMT
From: "Atlantis" <noeltd@hotmail.com>
Subject: Re: How do I loop through anonymous hash?
Message-Id: <sySPd.1780$ma4.677@newsfe2-gui.ntli.net>

Thank you to all of you, I can't quite believe I didn't try that (I know I
tried something like it:$), will have to stop taking those stupid pills.

Thank you all again:)




------------------------------

Date: Sun, 13 Feb 2005 21:52:10 GMT
From: Jazeker <jazeker_b_nospamlalala@yahoo.co.uk>
Subject: Re: How to navigate the docs?
Message-Id: <e_PPd.11127$iS3.673850@phobos.telenet-ops.be>

Kai Spitzley wrote:
> Mike <please@send.replies.to.ng> felt like saying:
> 
> 
>>What do you do when you want to find something in the Perl
>>documentation, and you have *no idea* in which of the many perlxxxx
>>it is?  I've tried perltoc in the past with little success.
> 
> 
> A man -k equivalent for Perl would a godsend.
> 

If you have a windows machine, install ActivePerl...  the html-ified 
docs are quite handy.

-- 
print <<EOF;
   Just a noobish Perl hacker
EOF


------------------------------

Date: Sun, 13 Feb 2005 15:14:28 +0100
From: Oliver Green <nospam@noglory.net>
Subject: Mod_perl: can I share a database connection by putting it in the startup-script?
Message-Id: <420f607d$0$28979$e4fe514c@news.xs4all.nl>

Hi,

I'm using Apache::Registry to speed up my cgi scripts. So far I've not 
modified the scripts themselves apart from moving most of the common 
"use <Module>" to the startup-script so Apache caches them.

As all scripts connect to a database (using DBI), I might as well move 
the connect statement to the startup-script. This works, and apparently 
the connection is now shared by all the children that Apache forks.

However, I have a hard time telling if it will work correctly under 
load. I suspect that by re-using the same connection over and over I 
might get some concurrency issues, or inconsistency errors.

How would I setup a test on my (modest) development system simulating 
high usage?

And I would love to hear from someone that actually knows if this 
connection sharing is at all advisable.

I even thought about sharing the statement handles (make a hash in the 
startup script, and add the statements with their handles and just
re-execute them), but haven't tried that yet.

Any info is appreciated,
Oliver Green



------------------------------

Date: 13 Feb 2005 18:48:51 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: naming a variable with the datum from another variable
Message-Id: <cuo7ej$agn$1@mamenchi.zrz.TU-Berlin.DE>

Chris Mattern  <matternc@comcast.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Chris Mattern  <matternc@comcast.net> wrote in comp.lang.perl.misc:
> >> Duncan Harris wrote:
> >> 
> >> > I need to create a separate associative array for each column of data
> > 
> >> You need to be using an array of hashes.
> >> 
> >> foreach my $i (@x) {
> >>   foreach my $j (sort(keys %$i)) {
> >                    ^^^^
> >>     do_something($i{$j});
> >>   }
> >> }
> > 
> > Why the "sort"?
> > 
> > Anno
> 
> Why not?  Sometimes it's nice to get the hash keys in 
> a predictable order.

Because it's sometimes nice is not a valid reason to invoke an n*log n
algorithm.  Other times it is utterly irrelevant.  There is nothing in
the problem to indicate one or the other.

The false idiom "sort keys ..." is all too popular.  We should not promote
its mindless application on clpm.

Anno


------------------------------

Date: 13 Feb 2005 06:11:16 -0800
From: ioneabu@yahoo.com
Subject: Re: Process word occurances in Bible?
Message-Id: <1108303876.137928.187470@z14g2000cwz.googlegroups.com>

> In this processing, convert all punctuation and numbers to blanks,
and
> uppercase to lower.
> One punctuation exception is '  (within a word)  is deleted leaving
wife's
> as  wifes
>
> When bible.txt is "thusly" processed I expect you shud get following
outputs
>  total words    = 789781
>  unique words =  12691
> xx.xx  Sec  ?.?? Ghz PC
>
> Pls post  your time and PC speed..

What did I do wrong here?  This is by his rules (I think):

#!/usr/bin/perl


use strict;
use warnings;

my $st = time();
my $fn = "kjv12.txt";
open (INF, $fn) or die "error: $!";
my %words;
for (<INF>)
{
    s/'//g;
    s/[^A-Za-z]/ /g;
    $words{lc $1}++ while /(\w+)/gi;
}
my ($unique, $total);
for (keys %words)
{
	$unique++ if $words{$_} == 1;
	$total++;
}
my $t = time()-$st;
print "Total = $total\n";
print "Unique = $unique\n";
print "Seconds = $t on 1.8ghz pentium 4\n";

output:

Total = 12691
Unique = 4004
Seconds = 3 on 1.8ghz pentium 4

wana



------------------------------

Date: 13 Feb 2005 06:36:22 -0800
From: ioneabu@yahoo.com
Subject: Re: Process word occurances in Bible?
Message-Id: <1108305382.697945.301160@f14g2000cwb.googlegroups.com>


> In this processing, convert all punctuation and numbers to blanks,
and
> uppercase to lower.
> One punctuation exception is '  (within a word)  is deleted leaving
wife's
> as  wifes
>
> When bible.txt is "thusly" processed I expect you shud get following
outputs
>  total words    = 789781
>  unique words =  12691
> xx.xx  Sec  ?.?? Ghz PC
>
> Pls post  your time and PC speed..

ok, got it now, sorry for wrong posting before:

#!/usr/bin/perl


use strict;
use warnings;

my $st = time();
my $fn = "kjv12.txt";
open (INF, $fn) or die "error: $!";
my %words;
my $total;
for (<INF>)
{
    s/'//g;
    s/[^A-Za-z]/ /g;
    $total++ and $words{lc $1}++ while /(\w+)/gi;
}
my $unique;
for (keys %words) {$unique++ if $words{$_}} ;
my $t = time()-$st;
print "Total = $total\n";
print "Unique = $unique\n";
print "Seconds = $t on 1.8ghz pentium 4\n";

output:

Total = 789781
Unique = 12691
Seconds = 4 on 1.8ghz pentium 4

wana (disregard last script)



------------------------------

Date: Sun, 13 Feb 2005 10:19:45 -0500
From: "David Frank" <dave_frank@hotmail.com>
Subject: Re: Process word occurances in Bible?
Message-Id: <420f700d$0$38924$ec3e2dad@news.usenetmonster.com>


<ioneabu@yahoo.com> wrote in message 
news:1108305382.697945.301160@f14g2000cwb.googlegroups.com...
>
>> In this processing, convert all punctuation and numbers to blanks,
> and
>> uppercase to lower.
>> One punctuation exception is '  (within a word)  is deleted leaving
> wife's
>> as  wifes
>>
>> When bible.txt is "thusly" processed I expect you shud get following
> outputs
>>  total words    = 789781
>>  unique words =  12691
>> xx.xx  Sec  ?.?? Ghz PC
>>
>> Pls post  your time and PC speed..
>
> ok, got it now, sorry for wrong posting before:
>
> #!/usr/bin/perl
>
>
> use strict;
> use warnings;
>
> my $st = time();
> my $fn = "kjv12.txt";
> open (INF, $fn) or die "error: $!";
> my %words;
> my $total;
> for (<INF>)
> {
>    s/'//g;
>    s/[^A-Za-z]/ /g;
>    $total++ and $words{lc $1}++ while /(\w+)/gi;
> }
> my $unique;
> for (keys %words) {$unique++ if $words{$_}} ;
> my $t = time()-$st;
> print "Total = $total\n";
> print "Unique = $unique\n";
> print "Seconds = $t on 1.8ghz pentium 4\n";
>
> output:
>
> Total = 789781
> Unique = 12691
> Seconds = 4 on 1.8ghz pentium 4
>
> wana (disregard last script)
>

Impressive,
however what if further processing is then desired, are the individual 
unique words, counts
available for searching etc..  in  their respective arrays?
They will be in other language's solutions.
Can you get a runtime with 4.xx  precision?





------------------------------

Date: Sun, 13 Feb 2005 21:20:34 GMT
From: Bob Walton <see.sig@rochester.rr.com>
Subject: Re: Process word occurances in Bible?
Message-Id: <CwPPd.6715$nC5.620@twister.nyroc.rr.com>

David Frank wrote:

 ...
> Can someone translate/process below so that I can post the source/results in 
> these 2 newsgroups?
 ...
FWIW:

use strict;
use warnings;
use Benchmark;
my $t0 = new Benchmark;
open IN,'bible12.txt' or
    die "Oops, couldn't open bible12.txt, $!";
my $txt;
{local $/;$txt=lc <IN>}
close IN;
$txt=~y/'//d;
my $words=my(@words)=$txt=~/([a-z]+)/g;
my %unique;
@unique{@words}=1;
my $unique=keys %unique;
print "total words=$words\nunique words=$unique\n";
my $t1 = new Benchmark;
my $td = timediff($t1, $t0);
print timestr($td),"\n";

d:\junk>perl junk517.pl
total words=789781
unique words=12691
18 wallclock secs (13.66 usr +  3.95 sys = 17.61 CPU)

1 GHz Athlon 100 MHz memory

Out of curiosity, I noted that over 95% of the time is spent on 
the match.  And that using \w+ instead of [a-z]+ costs about two 
seconds.
-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


------------------------------

Date: Sun, 13 Feb 2005 21:34:29 GMT
From: Bob Walton <see.sig@rochester.rr.com>
Subject: Re: Process word occurances in Bible?
Message-Id: <FJPPd.6719$nC5.1507@twister.nyroc.rr.com>

Bob Walton wrote:

> David Frank wrote:
> 
> ...
> 
>> Can someone translate/process below so that I can post the 
>> source/results in these 2 newsgroups?
> 
> ...

Hmmmm...running "ioneabu"'s program (with very slight 
modifications) shows that line-by-line processing is very much 
superior to processing a file slurp in a single string like I did 
(I'm surprised -- I'll have to remember this):

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;
my $st =new Benchmark;
my $fn = "bible12.txt";
open (INF, $fn) or die "error: $!";
my %words;
my $total;
for (<INF>)
{
     s/'//g;
     s/[^A-Za-z]+/ /g;
     $total++ and $words{lc $1}++ while /([a-z]+)/gi;
}
my $unique;
for (keys %words) {$unique++ if $words{$_}} ;
my $t = new Benchmark;
print "Total = $total\n";
print "Unique = $unique\n";
$t=timestr(timediff($t,$st));
print "Seconds = $t\n";

d:\junk>perl junk517a.pl
Total = 789781
Unique = 12691
Seconds =  6 wallclock secs ( 5.73 usr +  0.16 sys =  5.89 CPU)

This was on a 1 GHz Athlon with 100 MHz memory.
-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


------------------------------

Date: 14 Feb 2005 01:47:14 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Process word occurances in Bible?
Message-Id: <cuovv2$nkm$1@mamenchi.zrz.TU-Berlin.DE>

Bob Walton  <see.sig@rochester.rr.com> wrote in comp.lang.perl.misc:
> Bob Walton wrote:
> 
> > David Frank wrote:
> > 
> > ...
> > 
> >> Can someone translate/process below so that I can post the 
> >> source/results in these 2 newsgroups?
> > 
> > ...
> 
> Hmmmm...running "ioneabu"'s program (with very slight 
> modifications) shows that line-by-line processing is very much 
> superior to processing a file slurp in a single string like I did 
> (I'm surprised -- I'll have to remember this):

The program has a bug and is less efficient than it could be.

> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use Benchmark;
> my $st =new Benchmark;
> my $fn = "bible12.txt";
> open (INF, $fn) or die "error: $!";
> my %words;
> my $total;
> for (<INF>)

A for-loop also slurps the whole file.  Use a while-loop for line-by-line
processing.

> {
>      s/'//g;
>      s/[^A-Za-z]+/ /g;

If speed is an issue, prefer tr/// to s///.  lc() in the next line is
called for each word.  Another tr/// outside the loop can do the same.
I'd also split the line on white space instead of repeatedly matching.
What is the /i for?

>      $total++ and $words{lc $1}++ while /([a-z]+)/gi;

"$total++" and the count in %words should be independent statements,
not connected by "and".  If the first word in the file is a unique
word, you'll miss it. "$total++" will return 0 when it should be
counted.

> }
> my $unique;
> for (keys %words) {$unique++ if $words{$_}} ;

What on earth is this?

    my $unique = keys %words;

The total could also be calculated from the individual counts (provided
the counting bug is fixed):

    my $total = 0;
    $total += $_ for values %words;

Combining these suggestions should make it noticeably faster.

Anno


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 7776
***************************************


home help back first fref pref prev next nref lref last post