String Processing with Perl - Not My Type (Page 5 of 8 )
The chr() and ord() functions come in handy when converting from ASCII codes to characters and vice-versa. For example,
#!/usr/bin/perl
# returns "A"
print chr(65);
# returns 97
print ord("a");
If you prefer numbers to letters, you can use the hex() and oct() functions to convert between decimals, hexadecimals and octals.
#!/usr/local/bin/perl
#returns 170
print hex(AA);
# returns 40
print oct(50);
And if you ever find the need to reverse a string, well, you can always reach for the reverse() function...
#!/usr/bin/perl
$str = "Wassup, dood?";
# reverse string
# $rts now contains ?dood ,pussaW
$rts = reverse($str);
# returns "Sorry, you seem to be talking backwards - what does
# ?dood ,pussaW mean?"
print "Sorry, you seem to be talking backwards - what does $rts
mean?";
Next: Of Jumping Cows And Purple Pumpkins >>
More Perl Articles
More By Harish Kamath, (c) Melonfire