Quote:
Originally Posted by
james94538
I know how to use wc in unix to figure out words, characters, lines
but how about Perl, what approach should I take?
Lots of different approaches. I'll try some that won't win any obfuscation contests:
Lines:
Code:
my $cnt=0;
while ($_=<>) { $cnt++; }
print $cnt,"\n";
Words: (loosely defined as whitespace-delimited tokens)
Code:
my $cnt=0;
while ($_=<>) { $cnt+= scalar( split ); }
print $cnt,"\n";
Words: (defined as whitespace-delimited tokens containing a letter)
Code:
my $cnt=0;
while ($_=<>) { $cnt+= s/\b\S*[a-zA-Z]+\S*\b//; }
print $cnt,"\n";
Characters: (not including line-feeds, but including all others, ie, whitespace)
Code:
my $cnt=0;
while ($_=<>) { chop; $cnt+= length($_); }
print $cnt,"\n";
Characters: (not including whitespace)
Code:
my $cnt=0;
while ($_=<>) { chop; s/\s//g; $cnt+= length($_); }
print $cnt,"\n";