Counting characters

If I have  a string $whichis =  "hello,my,name"
how do I count the number of commas in $whichis ?
Who is Participating?
PC_User321Connect With a Mentor Commented:
>> .. pasting problem
Means "Ignore my first post".

Presumably @array contains a list of strings, and for each of these you want to know the number of commas.  Do it like this:

@array =  ("hello,my,name", "1,2,3,4,5");
foreach $Element (@array) {
   print "Number of commas in $Element is " . $Element =~ tr/,/,/ . "\n";

If you want the total of all the commas in @array, then add them up in the loop above

Hope this is what you want.
$Number = scalar(grep (/,/, $whichis));
Whoops, something went wrong with my paste:-

$whichis =  "hello,my,name";
$Number = $whichis =~ s/,/,/g;
benwilkinsonAuthor Commented:
I'm a little confused by the pasting problem

Also I forgot to mention that I am trying to do this within a cgi script on @values.

Where @values = $query->param($key);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.