Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Counting characters

Posted on 2000-03-05
4
Medium Priority
?
155 Views
Last Modified: 2010-03-05
If I have  a string $whichis =  "hello,my,name"
how do I count the number of commas in $whichis ?
0
Comment
Question by:benwilkinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2585928
$Number = scalar(grep (/,/, $whichis));
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2585934
Whoops, something went wrong with my paste:-

$whichis =  "hello,my,name";
$Number = $whichis =~ s/,/,/g;
0
 

Author Comment

by:benwilkinson
ID: 2585960
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);
0
 
LVL 5

Accepted Solution

by:
PC_User321 earned 100 total points
ID: 2586007
>> .. 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.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question