On Wed, Oct 13, 1999 at 01:59:00AM -0500, Richard L. Anderson wrote: } I am trying to use the tr function to count the frequency of letters } in a string and I am running into some unexpected behavior. Here's } my code: [snip] } } print "\nTrying it in a loop ...\n"; } foreach ('a','b','c') { } print "Frequency of $_ in $test: " . $test=~tr/$_/$_/ . "\n"; } } } [snip] } } Trying it in a loop ... } Frequency of a in aaabbbaabbccccacab: 0 } Frequency of b in aaabbbaabbccccacab: 0 } Frequency of c in aaabbbaabbccccacab: 0 } } Any ideas as to why $test=~tr/$_/$_/ doesn't work inside of a loop? Because the search list and replacement list are built at compile time, where $_ is undefined. There may be more than one way to do it, but this isn't one of them. See page 76 of the second edition Camel, and perlop. The way to do it is to use an eval. Here's one way which I came up with after much mucking around to avoid errors about modifying constant values. It works, but realize that I'm caffeine deprived at the moment, so there may be a better way: print "\nTrying it in a loop ...\n"; foreach ('a','b','c') { my $count; my ($oldlist, $newlist) = ($_,$_); { local($_) = $test; $count = eval "tr/$oldlist/$newlist/";} print "Frequency of $_ in $test: " . $count . "$@\n";; } } } --rich } } -- } Richard L. Anderson } Network Manager } School of Community Service, University of North Texas } <mailto:anderson@unt.edu> } -- Paul Schinder schinder@pobox.com # ===== Want to unsubscribe from this list? # ===== Send mail with body "unsubscribe" to macperl-request@macperl.org