On Fri, Jul 18, 2003 at 01:15:15PM -0500, Paul Archer wrote:
> Here's a quick way:
> perl -e '$var="abdaatela"; print ((scalar grep /a/,(split /(.)/,$var)),"\n");'
> 
> grep returns the number of matches in a scalar context, and the split breaks
> the string up into separate elements. The parens in the split return the
> item split on (otherwise it would throw away each character it encountered).

Okay, but "split //" is the usual idiom:

  perl -le 'print $n = grep /a/, split //, "abdaatela"'

And /./g is a bit shorter.

  perl -le 'print $n = grep /a/, /./g for "abdaatela"'

And of course, you could just match "a" in the first place.

  perl -le 'print $n = () = /a/g for "abdaatela"'

And tr/// is how you ought to count characters.

  perl -le 'print tr/a// for "abdaatela"'

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to