Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Char Count

by Anonymous Monk
on May 03, 2009 at 05:13 UTC ( [id://761559]=obfuscated: print w/replies, xml ) Need Help??

Count chars from STDIN, takes 1 command line argument that lists all characters to be counted. RegExp does all of the work
use re 'eval';$#ARGV<0&&exit 1;$s.=$_ while<STDIN>;$s=~/(?{$z=quotemeta shift@ARGV})((??{"[$z]"})(?{$c++})|.)*(?{print$c})/m;

Replies are listed 'Best First'.
Re: Char Count
by jwkrahn (Abbot) on May 03, 2009 at 18:28 UTC

    You are using the /m option but you are not using either of the ^ or $ anchors.   It looks like you should be using the /s option so that . will match a newline as well as other characters.

Re: Char Count
by afoken (Chancellor) on May 05, 2009 at 08:55 UTC
    $s.=$_ while <STDIN>;$s=~s/.../.../;

    ... lacks slurp mode and introduces an unneeded variable $s:

    undef$/;$_=<STDIN>;s/.../.../;
    $#ARGV<0&&exit 1;

    Much typing to test if @ARGV has elements, and exit 1 can be shortened to die, if the additional error message does not disturb:

    @ARGV||die;
    perl -e'use re "eval";';$#ARGV<0&&exit 1;...'

    ... can be shortened on the perl command line:

    perl -Mre=eval -e '@ARGV||die;...'

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://761559]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-26 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found