#!/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
In reply to Simple Matching by rnroot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |