in reply to Simple Matching

How about something like this

sub count { # pass it the long sentence and the match word/char! my ($LONG,$CHAR) = @_; my $count = 0; while ($LONG=~m/$CHAR/) { $LONG=~s/$CHAR//; # remove match? $count++; } return $count; }

Then you can call it in your code like this

foreach ( @check ) { print "[$_]: " . count($string,$_); }

Well, it looks a bit messy, but it does exactly what you need with high performance.

Replies are listed 'Best First'.
Re^2: Simple Matching
by rnroot (Initiate) on Sep 28, 2009 at 07:29 UTC
    Ahmad, Thanks. That is not messy and works. Similar to the "Method 1" that I used. The "brute force" method gained a factor of 50 in time on the loop. Your loop (similar to my first go at it) gained a factor of 2. To the mods: I hope I am not getting off track. I have found responses quite helpful. Ugly code can work faster than pretty code, but sometimes perl can be unintelligable, short and extremely fast.