?
Solved

Counting characters

Posted on 2000-03-05
4
Medium Priority
?
154 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

752 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