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 Java

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

From Dev

How to log a variable's name and value?

From Dev

Setting Localstorage Variable Name to a Variable's value

From Dev

How to get a constant's name given its value

From Dev

Ternary operator to set value based on value of variable

From Dev

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

From Dev

Coldfusion Reassign Session Variable Based on its Value

From Dev

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

From Dev

Python: Print a variable's name and value?

From Dev

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

From Dev

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

From Dev

How to print the variable name along with its value?

From Dev

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

From Dev

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

From Dev

Assign value to variable based on its annotation

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Python: Calling a class based on variable's value

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Using sed to replace variable name and its value

From Dev

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

From Dev

Access Perl variable with it's package name in scalar

From Dev

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

From Dev

Want to Access variable by its name with string value

Related Related

  1. 1

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

  2. 2

    How to log a variable's name and value?

  3. 3

    Setting Localstorage Variable Name to a Variable's value

  4. 4

    How to get a constant's name given its value

  5. 5

    Ternary operator to set value based on value of variable

  6. 6

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

  7. 7

    Coldfusion Reassign Session Variable Based on its Value

  8. 8

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

  9. 9

    Python: Print a variable's name and value?

  10. 10

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

  11. 11

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

  12. 12

    How to print the variable name along with its value?

  13. 13

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

  14. 14

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

  15. 15

    Assign value to variable based on its annotation

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    Python: Calling a class based on variable's value

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

    Using sed to replace variable name and its value

  26. 26

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

  27. 27

    Access Perl variable with it's package name in scalar

  28. 28

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

  29. 29

    Want to Access variable by its name with string value

HotTag

Archive