tchrist@convex.COM (Tom Christiansen) (12/21/90)
From the keyboard of bzs@world.std.com (Barry Shein):
:Imagine, in perl, if you could insert any expression midway into a
:pattern so whenever whitespace was hit you could increment $words
:right there, let's say "/(\S+)@$words++@/" was a pattern which
:incremented $words every time a space run was found, that's a common
:thing in snobol. Your example is darn close, tho, at least the loop
:has been eliminated via the use of /g, that was the spirit of the
:thing.
:
:Just various ways to find mapcar nirvana...
(Barry, are you sure you're not just leading me on? :-)
Permit me to introduce you to an eval in another guise, the /e modifier:
[this one even works -- the last one had a bug. :-(]
0 #!/usr/bin/perl -n
1 $chars += length;
2 s/\S+/$words++/eg;
3 next unless eof;
4 printf "%8d %8d %8d %s\n", $., $words, $chars, ($ARGV eq '-'?'':$ARGV);
5 $tlines += $.; $twords += words; $tchars += $chars; reset 'wc'; $. = 0;
6 next unless $files++ && eof();
7 printf "%8d %8d %8d %s\n", $tlines, $twords, $tchars, "total";
While evals are pretty neat, this one does slow things down a lot. Putting
it in the line loop like that makes this program run 3.5 times longer
than with line two as simply:
$words += s/\S+//g;
Better to count when all done in this case, but there are lots of more
complex and interesting things you can do with /e easily. Let's say you
also want to create a count of all the distinct words and then print them
out in descending numeric order by count at the end of each file:
#!/usr/bin/perl -n
s/\S+/$saw{$&}++/eg;
next unless eof;
sub down { $saw{$b} <=> $saw{$a}; }
for (sort down keys %saw) { printf "%8d %s\n", $saw{$_}, $_; };
Is that the kind of thing you are looking for, Barry?
We've strayed pretty from religion here (I think:-), so I'm redirecting
followups back into comp.lang.perl.
--tom
--
Tom Christiansen tchrist@convex.com convex!tchrist
"With a kernel dive, all things are possible, but it sure makes it hard
to look at yourself in the mirror the next morning." -me