Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Char Count

by Anonymous Monk
on May 03, 2009 at 05:13 UTC ( #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;

Comment on Char Count
Download Code
Replies are listed 'Best First'.
Re: Char Count
by jwkrahn (Monsignor) 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 (Prior) 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
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? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2015-07-30 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls