so...this is suposed to count the words in FILE and return how many occourances of
each word there were...its not working for me thoughits only returning the count
for the last word in the file...help
#!/usr/local/bin/perl
open (FILE,"../www/main.php3");
@lines=;
close(FILE);
foreach $i (
: so...this is suposed to count the words in FILE and return how many occourances of
:each word there were...its not working for me thoughits only returning the count
:for the last word in the file...help
Think: In the first loop, what happens to the first line when you move
to the second?
On Tue, Apr 24, 2001 at 10:17:02AM -0500, Chris Brown wrote:
: so...this is suposed to count the words in FILE and return how many occourances of
:each word there were...its not working for me thoughits only returning the count
:for the last word in the file...help
The following is a genera
The problem is that you override the (global) array @words for each line.
You go through @lines and the split overrides @words!
While we are at it... ;-)
You do not need that many loops. The programm will be much simpler like
that:
open(FILE, "yourFileName");
while ($line = ) { #no need to read
At 11:17 AM 4/24/2001, you wrote:
>so...this is suposed to count the words in FILE and return how many
>occourances of each word there were...its not working for me thoughits
>only returning the count for the last word in the file...help
>
>#!/usr/local/bin/perl
>
>open (FILE,"../www/main.ph
--- Chris Brown <[EMAIL PROTECTED]> wrote:
> so...this is suposed to count the words in FILE and return how many
> occourances of each word there were...its not working for me
> thoughits only returning the count for the last word in the
> file...help
>
> #!/usr/local/bin/perl
>
> open (FIL
Hi Chris,
You are getting only the last line of the file because of this:
> foreach $i (@lines) {
> @words = split(/\s+/, $i);
> }
You reassign the @words array each time, and end up with the last line only
when exiting the foreach loop. You may want to look at 'perldoc -f push' to see
how to a
Paul ([EMAIL PROTECTED]) wrote:
>
> #!/usr/local/bin/perl -w
>
> use strict
> open (FILE,$0) or die $!; # this reads itself
> my($data,%count);
> { local $/ = undef; # erases the record seperator for this block
>$data = ; # slurps in the whole file to $data
> }
> cl
Agreed, and thanks for pointing that out!
--- Michael Lamertz <[EMAIL PROTECTED]> wrote:
> Paul ([EMAIL PROTECTED]) wrote:
> >
> > #!/usr/local/bin/perl -w
> >
> > use strict
> > open (FILE,$0) or die $!; # this reads itself
> > my($data,%count);
> > { local $/ = undef; # erases t
CTED]>
To: <[EMAIL PROTECTED]>
Cc: "Chris Brown" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, April 24, 2001 1:03 PM
Subject: Re: [BPQ] help!! any idea whats wrong with this??
> Paul ([EMAIL PROTECTED]) wrote:
> >
> > #!/usr/local/bin
" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Cc: "Chris Brown" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
> Sent: Tuesday, April 24, 2001 1:03 PM
> Subject: Re: [BPQ] help!! any idea whats wrong with this??
>
>
> > Paul ([EMA
; map { $count{$_}++ } $data =~ /(\w+)/sog;
>
> try
>$count{$_}++ } for $data =~ /([\w-]+)/sog;
ps^Make that:
$count{$_}++ for $data =~ /([\w-]+)/sog;
> > - Original Message -
> > From: "Michael Lamertz" <[EMAIL PROTE
;[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Thursday, April 26, 2001 6:59 PM
Subject: Re: [BPQ] help!! any idea whats wrong with this??
>
> --- Paul <[EMAIL PROTECTED]> wrote:
> >
> > --- Bill Lawry <[EMAIL PROTECTED]> wrote:
> > > Pretty coo
-
> From: "Paul" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>; "Bill Lawry" <[EMAIL PROTECTED]>
> Cc: <[EMAIL PROTECTED]>
> Sent: Thursday, April 26, 2001 6:59 PM
> Subject: Re: [BPQ] help!! any idea whats wrong with this??
>
>
: Because someone (and with apologies to all, I don't recall off the top
: of my head who)correctly pointed out to me earlier in this thread that
: using map() here was inefficient. map() builds and returns an array, so
: there's no point in using it in this void context. Aside from that,
: both
Timothy Kimball ([EMAIL PROTECTED]) wrote:
>
> : Because someone (and with apologies to all, I don't recall off the top
> : of my head who)correctly pointed out to me earlier in this thread that
> : using map() here was inefficient. map() builds and returns an array, so
> : there's no point in us
: Ah, a Heisenbug. There's a problem with your benchmarking:
Yep, you're right. map is slightly slower when it actually has
something to do. I stand corrected... again.
So the moral of the story is: If you want your code
to run really fast, make it do nothing. ;)
That's what I love about Per
17 matches
Mail list logo