rnroot has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl # Perl 5.10.1 built for MSWin32-x86-multi-thread # Count number each @check value occur in $string # Two Methods: Long way (WORKS), Shorter way (PROBLEM) use strict; use warnings; my @check = ("A", "B"); my $string = "2A 3B 4B"; my $count; # how many times does "A" appear $count = $string =~ tr/A//; print "[A]: $count "; # how many times does "B" appear $count = $string =~ tr/B//; print "[B]: $count "; print "\n"; # returns "[A]: 1 [B]: 2" CORRECT # Shorten the above foreach ( @check ) { $count = $string =~ tr/$_//; print "[$_]: $count "; } print "\n"; # returns "[A]: 0 [B]: 0" INCORRECT # What is going on? Why doesn't the "tr" work? exit; # FOOBAR
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple Matching
by gwadej (Chaplain) on Sep 28, 2009 at 04:28 UTC | |
|
Re: Simple Matching
by BrowserUk (Patriarch) on Sep 28, 2009 at 04:44 UTC | |
by rnroot (Initiate) on Sep 28, 2009 at 05:04 UTC | |
by ikegami (Patriarch) on Sep 28, 2009 at 05:14 UTC | |
by rnroot (Initiate) on Sep 28, 2009 at 06:11 UTC | |
by AnomalousMonk (Archbishop) on Sep 28, 2009 at 12:42 UTC | |
| |
by BrowserUk (Patriarch) on Sep 29, 2009 at 00:54 UTC | |
|
Re: Simple Matching
by ahmad (Hermit) on Sep 28, 2009 at 06:42 UTC | |
by rnroot (Initiate) on Sep 28, 2009 at 07:29 UTC | |
|
Re: Simple Matching
by johngg (Canon) on Sep 28, 2009 at 11:38 UTC |