• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Counting characters

If I have  a string $whichis =  "hello,my,name"
how do I count the number of commas in $whichis ?
  • 3
1 Solution
$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);
>> .. 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.
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now