Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl?

Can someone please explain what tr#A-Za-z0-9+/# -_#; means in perl?
# 2  
Looks like a base64 encoding thing.
# 3  
prolly not.
Code:
tr#A-Za-z0-9+/# -_#;

== turn an alphanumeric string (if that is something you know) into '-_' where _
is a reserved word in perl regex that means a word separator. i.e., replace alphanum words
witj the hyphen
# 4  
That's not really the case here.

Perl expands character ranges specified by the hyphen character ("-") in the tr operator.

So, in this expression:

Code:
tr#A-Za-z0-9+/# -_#

the SEARCHLIST (in red) is just a compact form of

(a) all characters from "A" to "Z", and
(b) all characters from "a" to "z", and
(c) all characters from "0" to "9", and
(d) the characters "+" and "/"

i.e. this -

Code:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

The REPLACEMENTLIST (in green) is a compact form of

(a) all characters from space (" ") to underscore character ("_")

i.e. this -

Code:
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_

And the tr operator simply transliterates all characters in SEARCHLIST with the corresponding character in REPLACEMENTLIST.

The mapping could be seen better this way -

Code:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_

You can test this like so -

Code:
$
$
$ # "ABCD" maps to "space, exclamation mark, double quote, hash character" therefore
$
$ echo "ABCD" | perl -plne 'tr#A-Za-z0-9+/# -_#'
 !"#
$
$
$ # "0123" maps to "TUVW"
$
$ echo "0123" | perl -plne 'tr#A-Za-z0-9+/# -_#'
TUVW
$
$
$ # "c" maps to "<", "+" maps to "^", "t" maps to "M", so "c+t" maps to "<^M"
$
$ echo "c+t" | perl -plne 'tr#A-Za-z0-9+/# -_#'
<^M
$
$
$ # any other character is left as it is
$
$ echo "$~%=" | perl -plne 'tr#A-Za-z0-9+/# -_#'
$~%=
$
$
$ # check the entire SEARCH list, with unmapped character "~" at the end
$
$ echo "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~" | perl -plne 'tr#A-Za-z0-9+/# -_#'
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_~
$
$
$

HTH,
tyler_durden
These 3 Users Gave Thanks to durden_tyler For This Post:
# 5  
Yes, after the replacement, you can take away the lower 6 bits for your base64 to binary conversion.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl/Array Sorting : Can someone please explain below code $h{$_}++

sub uniq { my %h; return grep { !$h{$_}++ } @_ } The above code is to remove duplicates from array. I am having hard time understanding below things (basically around highlighted code in bold)- when was the value inserted in hash? and are we only adding a key in Hash not... (1 Reply)
Discussion started by: Tanu
1 Replies

2. UNIX for Beginners Questions & Answers

Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted"

@sorted = sort { $a <=> $b } @unsorted; I am having hard time understanding how this works? I know the output but interested to know the working. Thanks in advance. (2 Replies)
Discussion started by: Tanu
2 Replies

3. Shell Programming and Scripting

can you explain this perl command?

I am using this line of perl code to change the file format and remove ^M at the end of each line in files: perl -i -pe's/\r$//;' <name of file here> Can you explain to me what this code does, and translate it into bash/awk/sed? (2 Replies)
Discussion started by: locoroco
2 Replies

4. UNIX for Dummies Questions & Answers

Explain perl formatting commands

Could you please tell the meaning of Below mentioned perl script lines: format OWNER_TOTAL = @< Owner Code @<<<<<<total: @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @###### #.## $prev_cust_type, $prev_owner_code,... (2 Replies)
Discussion started by: vinothrajan55
2 Replies

5. UNIX for Advanced & Expert Users

What does "__FD_PRN_" means in perl code ?

I have seen something like this in a perl code: $_ =~ s/__FD_PRN_/\\(/g What does this "__FD_PRN_" means. I have searched google but was not able to find any info regarding this. Appreciate if some one can refer to a link for these characters. From comments/code it used to substitue "(" with... (3 Replies)
Discussion started by: sarbjit
3 Replies

6. Shell Programming and Scripting

perl: Please explain this for me: $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];

This is taken from perlop. I can't understand what's going on, please can someone explain this for me? $hexdigit = (0 .. 9, 'a' .. 'f'); to get a hexadecimal digit (2 Replies)
Discussion started by: ksheller
2 Replies

7. UNIX for Advanced & Expert Users

perl explain syntax !!!

hi all i was going through some perl code i came across this line and i am not getting what is exactly going on .. $$this{localtion} = GetName->GetVarName("EXE_DIR") ; what is the red part doing in above code (2 Replies)
Discussion started by: zedex
2 Replies

8. UNIX for Dummies Questions & Answers

$? means?

Hi All, Can you please explain wats the meaning of this $? in unix Thanks (2 Replies)
Discussion started by: mathur
2 Replies

9. Shell Programming and Scripting

perl doubt plz explain....

hi all i wrote a shell script which uses perl script my code is : >cat filename | while read i >do >perl -e 'require "/home/scripts/abc.pl" ; abc("$i")' >done perl script used will simply check syntax of Cobol programs but it didn't work for me so i asked my colleague he suggested... (1 Reply)
Discussion started by: zedex
1 Replies

Featured Tech Videos