b.sabarathinam saba b.sabarathinam saba - 1 year ago 65
Perl Question

In map , whether the tr operator will affect the original array?

I wrote the code like this,

my (@Last,@Input);
@Input=qw(This is FOR Testing);
map{ tr/A-Z/a-z/;tr/a-z//cd;push @Last,$_;} @Input;
print "Original array ==>@Input<===\n";
print "Modified array ==>@Last<===\n";

Once I executed the code, I got the output like this,

Original array ==>this is for testing<===
Modified array ==>this is for testing<===

Whether the tr operator will affect the original array?

Answer Source

tr/// is destructive by default.

To preserve the original string you should use the /r modifier.

From perldoc perlop:

If the /r (non-destructive) option is present, a new copy of the string is made and its characters transliterated, and this copy is returned no matter whether it was modified or not

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download