Solved

Counting characters

Posted on 2000-03-05
4
152 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 25 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 106
perl script to check whether folder contains any files 5 107
rename outfile before writing 2 77
read an xml file in perl 2 86
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

732 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