Perl tr operator is transliterating based on the variable's name not its value

rbaumann

I'm using Perl 5.16.2 to try to count the number of occurrences of a particular delimiter in the $_ string. The delimiter is passed to my Perl program via the @ARGV array. I verify that it is correct within the program. My instruction to count the number of delimiters in the string is:

$dlm_count = tr/$dlm//;

If I hardcode the delimiter, e.g. $dlm_count = tr/,//; the count comes out correctly. But when I use the variable $dlm, the count is wrong. I modified the instruction to say

$dlm_count = tr/$dlm/\t/;

and realized from how the tabs were inserted in the string that the operation was substituting every instance of any of the four characters "$", "d", "l", or "m" to \t — i.e. any of the four characters that made up my variable name $dlm.

Here is a sample program that illustrates the problem:

$_ = "abcdefghij,klm,nopqrstuvwxyz";
my $dlm = ",";
my $dlm_count = tr/$dlm/\t/;
print "The count is $dlm_count\n";
print "The modified string is $_\n";

There are only two commas in the $_ string, but this program prints the following:

The count is 3  
The modified string is abc      efghij,k                ,nopqrstuvwxyz

Why is the $dlm token being treated as a literal string of four characters instead of as a variable name?

zdim

You cannot use tr that way, it doesn't interpolate variables. It runs strictly character by character replacement. So this

$string =~ tr/a$v/123/

is going to replace every a with 1, every $ with 2, and every v with 3. It is not a regex but a transliteration. From perlop

Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation. That means that if you want to use variables, you must use an eval():

eval "tr/$oldlist/$newlist/";
die $@ if $@;
eval "tr/$oldlist/$newlist/, 1" or die $@;

The above example from docs hints how to count. For $dlms in $string

$dlm_count = eval "\$string =~ tr/$dlm//";  

The $string is escaped so to not be interpolated before it gets to eval. In your case

$dlm_count = eval "tr/$dlm//";

You can also use tools other than tr (or regex). For example, with string being in $_

my $dlm_count = grep { /$dlm/ } split //;

When split breaks $_ by the pattern that is empty string (//) it returns the list of all characters in it. Then the grep block tests each against $dlm so returning the list of as many $dlm characters as there were in $_. Since this is assigned to a scalar, $dlm_count is set to the length of that list, which is the count of all $dlm.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Perl tr operator is transliterating based on the variable's name not its value

From Dev

How to get a variable's name based on its value

From Dev

VBScript - Create a dynamic variable name based on another variable's value

From Dev

Ternary operator to set value based on value of variable

From Dev

How to get a name of a number based on its value?

From Dev

How to print the variable name along with its value?

From Dev

Using sed to replace variable name and its value

From Dev

Want to Access variable by its name with string value

From Dev

Coldfusion Reassign Session Variable Based on its Value

From Dev

Assign value to variable based on its annotation

From Dev

Access Perl variable with it's package name in scalar

From Dev

Change value of check box when its name is the value of a variable

From Dev

Setting Localstorage Variable Name to a Variable's value

From Dev

Convert scalar variable and its value to hash ref variable's value

From Dev

Not being able to assign JSON value to a an Element based on its name

From Dev

Passing a variable value, rather than its name, as a class

From Dev

How can I assign a value to a variable by passing a string of its name?

From Dev

return the numeric value of a indexed variable rather than its name

From Dev

Passing a variable value, rather than its name, as a class

From Dev

How to convert text into variable name to print its value using innerHTML?

From Dev

How to pass the name of variable to a function and return its value in C#?

From Dev

How to get the corresponding variable name to its corresponding value C#

From Dev

How to log a variable's name and value?

From Dev

Python: Print a variable's name and value?

From Dev

Using a variable's value to refer to a variable's name in C++

From Dev

Python: Calling a class based on variable's value

From Dev

How to get a constant's name given its value

From Dev

Access package variable after its name is removed from symbol table in Perl?

From Java

How to sort an object based on its value's length

Related Related

  1. 1

    Perl tr operator is transliterating based on the variable's name not its value

  2. 2

    How to get a variable's name based on its value

  3. 3

    VBScript - Create a dynamic variable name based on another variable's value

  4. 4

    Ternary operator to set value based on value of variable

  5. 5

    How to get a name of a number based on its value?

  6. 6

    How to print the variable name along with its value?

  7. 7

    Using sed to replace variable name and its value

  8. 8

    Want to Access variable by its name with string value

  9. 9

    Coldfusion Reassign Session Variable Based on its Value

  10. 10

    Assign value to variable based on its annotation

  11. 11

    Access Perl variable with it's package name in scalar

  12. 12

    Change value of check box when its name is the value of a variable

  13. 13

    Setting Localstorage Variable Name to a Variable's value

  14. 14

    Convert scalar variable and its value to hash ref variable's value

  15. 15

    Not being able to assign JSON value to a an Element based on its name

  16. 16

    Passing a variable value, rather than its name, as a class

  17. 17

    How can I assign a value to a variable by passing a string of its name?

  18. 18

    return the numeric value of a indexed variable rather than its name

  19. 19

    Passing a variable value, rather than its name, as a class

  20. 20

    How to convert text into variable name to print its value using innerHTML?

  21. 21

    How to pass the name of variable to a function and return its value in C#?

  22. 22

    How to get the corresponding variable name to its corresponding value C#

  23. 23

    How to log a variable's name and value?

  24. 24

    Python: Print a variable's name and value?

  25. 25

    Using a variable's value to refer to a variable's name in C++

  26. 26

    Python: Calling a class based on variable's value

  27. 27

    How to get a constant's name given its value

  28. 28

    Access package variable after its name is removed from symbol table in Perl?

  29. 29

    How to sort an object based on its value's length

HotTag

Archive