--- Bill Lawry <[EMAIL PROTECTED]> wrote:
> Neato & thanks.
>
> I don't understand why one solution uses map & count and the other
> just uses count. Is map implied in the second solution?
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 do the same thing. The postfix for is cleaner. =o)
> > > map { $count{$_}++ } $data =~ /(\w+)/sog;
> > $count{$_}++ for $data =~ /([\w-]+)/sog;
btw, "foreach" might have been more readable here, but "foreach" is
pretty much an alias to "for", which is fewer characters to type....
~wink~
> ----- Original Message -----
> 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??
>
>
> >
> > --- Paul <[EMAIL PROTECTED]> wrote:
> > >
> > > --- Bill Lawry <[EMAIL PROTECTED]> wrote:
> > > > Pretty cool but when used on a file it breaks hyphenated words
> into
> > > > their components and counts them separately:
> > > >
> > > > 17 occurrences of 'Acct'
> > > > 3 occurrences of 'Authentic'
> > > > etc
> > > >
> > > > instead of:
> > > >
> > > > 3 occurrences of Acct-Authentic
> > > > 3 occurrences of Acct-Delay-Time
> > > > 1 occurrences of Acct-Input-Octets
> > > > 1 occurrences of Acct-Input-Packets
> > > > 1 occurrences of Acct-Output-Octets
> > > > 1 occurrences of Acct-Output-Packets
> > > > 3 occurrences of Acct-Session-Id
> > > > 1 occurrences of Acct-Session-Time
> > > > 3 occurrences of Acct-Status-Type
> > >
> > > Not what you want? Then let's edit the pattern. =o)
> > >
> > > Instead of
> > > map { $count{$_}++ } $data =~ /(\w+)/sog;
> > >
> > > try
> > > $count{$_}++ } for $data =~ /([\w-]+)/sog;
> >
> > OOOOps............^ Make that:
> >
> > $count{$_}++ for $data =~ /([\w-]+)/sog;
> >
> >
> > > > ----- Original Message -----
> > > > From: "Michael Lamertz" <[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 ([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 = <FILE>; # slurps in the whole file to
> $data
> > > > > > }
> > > > > > close(FILE); # good habit
> > > > > > map { $count{$_}++ } $data =~ /(\w+)/sog; # watch the
> context!
> > > > > > print map { "$count{$_} occurances of '$_'\n" } sort keys
> > > > %count;
> > > > > >
> > > > > > Perl is a wonderfully concise language.
> > > > > > The above is strictly given as an example of a few
> performance
> > > > tricks
> > > > > > that are worth researching. =o)
> > > > >
> > > > > I agree printing the map output, but I disagree using map to
> > > > calculate
> > > > > the sums. map always generates a new array that immediately
> gets
> > > > dumped
> > > > > since it's not assigned. A foreach would be nicer to system
> > > > resources
> > > > > and better to read. To make it short, use it postfix:
> > > > >
> > > > > $count{$_}++ foreach ($data=~ /.../);
> > > > >
> > > > > Check 'perldoc perlfaq6' for reference.
> > > > >
> > > > > --
> > > > > If we fail, we will lose the war.
> > > > >
> > > > > Michael Lamertz | [EMAIL PROTECTED] /
> > > > [EMAIL PROTECTED]
> > > > > Nordstr. 49 | http://www.lamertz.net
> > > > > 50733 Cologne | Work: +49 221 3091-121
> > > > > Germany | Priv: +49 221 445420 / +49 171
> 6900
> > > 310
> > > > >
> > > >
> > >
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Yahoo! Auctions - buy the things you want at great prices
> > > http://auctions.yahoo.com/
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Yahoo! Auctions - buy the things you want at great prices
> > http://auctions.yahoo.com/
> >
>
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/