Solved

Counting characters

Posted on 2000-03-05
4
146 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 101
Is it possible to get a piece of info in a line from a big text file with perl? 7 72
syslog unix file 20 76
Perl string filter 5 81
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

837 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