Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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;

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 (Canon) on May 05, 2009 at 08:55 UTC
    $s.=$_ while <STDIN>;$s=~s/.../.../;

    ... lacks slurp mode and introduces an unneeded variable $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:

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

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

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


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2020-12-02 00:07 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (25 votes). Check out past polls.