×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

counting matches in string w/o s/// ?

counting matches in string w/o s/// ?

counting matches in string w/o s/// ?

(OP)
Hi, everyone
I want to count the number of occurrences my pattern matches in a given string without using s///.  I don't need to substitute or translate.  I just need to count the matches.

For example:

$someStr = "hihihi";
$someStr =~ /hi/;  # evaluates to true not 3
$someStr =~ s/hi/hi/g;  # evaluates to 3 but unnecessary substitution and s/// operator will not work with read only values

any suggestions how to do this efficiently with read only values, too?

Thanks,
Keith

RE: counting matches in string w/o s/// ?

$someStr = "hihihi";
$count = $someStr;
$count = int($count =~ s/hi//g;)

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: counting matches in string w/o s/// ?

(OP)
Yeah, but you still use s/// ...
in fact, you could just do

$someStr = "hihihi";
$count = ($someStr =~ s/hi/hi/g);

or

$someStr = "hihihi";
$count = ($someStr =~ s/(hi)/$1/g);

but both still use s///;

I don't think there's an easier way, not that the ones above are hard.  If only m// returned the number of matches instead of true or false, that would be much more useful (esp. since 0 evals to false and any other number to true)
 

RE: counting matches in string w/o s/// ?

Well, it at least keeps the original variable as read-only.  The string is left intact while you also compute the integer.  What else do you need?  I don't think there is another way to do it.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close