writes:
> I created two subroutines, I want to know if there is a better, faster
> way to do this. I just use ord() and chr():
>
> 1) Take a upper case string like "GRABER" and covert it to its ASCII
> equivalent (a string of 2 digit numbers, concatenated) - 718265666982
>
> 2) Take a string like "718265666982" and convert it to a string of
> characters like "GRABER"
Perl is faster when iteration is implicit.
s/(.)/f($1)/g; # iterates by character nicely.
#!/usr/local/bin/perl
use warnings;
use strict;
my (%H,%h);
%H = reverse (%h=qw( G 71 R 82 A 65 B 66 E 69));
$_ = "GRABER\n";
print;
s/(.)/$h{$1}/g; # string to ASCII concatenated
print;
s/(..)/$H{$1}/g; # ASCII concat string to ASCII
print;
# prints
#GRABER
#718265666982
#GRABER
Note that dot (.) did not match \n at end of input line
These also work, for the given input string.
s/(.)/ord($1)/ge; # string to ASCII concatenated
s/(..)/chr($1)/ge; # and back
Perl is faster when iteration is implicit.
--
Joel