Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Strange behaviour of tr function in case the set1 is supplied by a variable

by roboticus (Chancellor)
on Nov 16, 2017 at 03:08 UTC ( [id://1203545]=note: print w/replies, xml ) Need Help??


in reply to Strange behaviour of tr function in case the set1 is supplied by a variable

likbez:

Feature, per the tr docs Characters may be literals or any of the escape sequences accepted in double-quoted strings. But there is no interpolation, so "$" and "@" are treated as literals. A hyphen at the beginning or end, or preceded by a backslash is considered a literal. Escape sequence details are in the table near the beginning of this section.

So if you want to use a string to specify the values in a tr statement, you'll probably have to do it via a string eval:

$ cat foo.pl use strict; use warnings; my $str1 = 'abcde'; my $str2 = 'eda'; my $diff1 = 0; eval "\$diff1=\$str1=~tr/$str2//"; print "diff1: $diff1\n"; $ perl foo.pl diff1: 3

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1203545]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found