# counting word occurances - Page 2

•  Subject
• Author
• Posted on

## Re: counting word occurances

Jürgen Exner wrote:

That FAQ entry comments on the s/// operator. Is that applicable to
capturing a value via the m// operator too?

while (<LOG>) { /(\S(?:.*\S))/ and \$cnt++ or next }

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

## Re: counting word occurances

Judging from Sinan's benchmark it's not.

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

## Re: counting word occurances

trying

See Chaper 17, Exercise 2, in Learning Perl, 2ed.
--
Bill Segraves

## Re: counting word occurances

See Chapter 17, Exercise 2, p. 176, in Learning Perl, 2nd ed., as well as
"one way to do it" in Appendix A, p. 233 .
--
Bill Segraves

## Re: counting word occurances

Rodrick Brown (rodrick.brown@gmail.com) wrote on MMMMCCXCIV September
[]  Hello,
[]
[]  Just learning Perl so bare with me.
[]
[]  I have the following output file:
[]
[]  pear
[]   apple
[]  apple
[]     orange
[]  mango
[]  mango
[]          pear
[]     cherry
[]  apple
[]
[]  ill would like the count the ammount of occurances for each fruit.

No need for perl here. Just some standard shell tools will do a fine job:

grep -v '^ ' input_file | sort | uniq -c

Abigail
--
my \$qr =  qr/^.+?(;).+?|;Just another Perl Hacker;|;.+\$/;
\$qr =~  s/\$qr//g;
print \$qr, "\n";

## Re: counting word occurances

No, that will not count " apple" and "   orange" or any other lines
starting
with one or more spaces. This will work better:

perl -ple 's/^\s+//' input_file | sort | uniq -c

Or:

perl -nle 's/^\s+//;\$c++;END{printf("%6d %s\n",\$_,\$c) for
sort<=>\$c}keys%c}' input_file

--
Kjetil Skotheim

## Re: counting word occurances

Kjetil Skotheim (dont@mail.me) wrote on MMMMCCXCVI September MCMXCIII in
==
== > Rodrick Brown (rodrick.brown@gmail.com) wrote on MMMMCCXCIV September
== > []  Hello,
== > []
== > []  Just learning Perl so bare with me.
== > []
== > []  I have the following output file:
== > []
== > []  pear
== > []   apple
== > []  apple
== > []     orange
== > []  mango
== > []  mango
== > []          pear
== > []     cherry
== > []  apple
== > []
== > []  ill would like the count the ammount of occurances for each fruit.
== >
== >
== > No need for perl here. Just some standard shell tools will do a fine job:
== >
== >     grep -v '^ ' input_file | sort | uniq -c
== >
== > Abigail
==
==  No, that will not count " apple" and "   orange" or any other lines
==  starting
==  with one or more spaces.

Oh, I though that was the intention of the OP.

==                           This will work better:
==  perl -ple 's/^\s+//' input_file | sort | uniq -c
==

sed -e 's/^ *//' input_file | sort | uniq -c

Abigail
--
sub _ {\$_ = shift and y/b-yB-Y/a-yB-Y/                xor      !@ _?
exit print                                                  :
print and push @_ => shift and goto &{(caller (0)) [3]}}
split // => "KsvQtbuf fbsodpmu\ni flsI "  xor       & _

## Re: counting word occurances

Abigail (abigail@abigail.nl) wrote on MMMMCCXCVI September MCMXCIII in
::  Kjetil Skotheim (dont@mail.me) wrote on MMMMCCXCVI September MCMXCIII in
:: ==
:: == >
:: == >     grep -v '^ ' input_file | sort | uniq -c
:: == >
:: ==
:: ==  No, that will not count " apple" and "   orange" or any other lines
:: ==  starting
:: ==  with one or more spaces.
::
::
::  Oh, I though that was the intention of the OP.

And, indeed, it was. From the OP's attempt:

while(<LOG>)
{
next if(/^\s+/);
push(@fruits,\$_);
}

It skips all lines starting with whitespace.

Abigail
--
#!/opt/perl/bin/perl -w
\$\ = \$"; \$SIG = sub {print and exit};
kill 15 => fork for qw /Just another Perl Hacker/;