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