PERL first two characters subsitutions

I am looking for a PERL expression to only replace the first two characters with another character. The first two characters can range from 10 to 35 and will be replaced by a letter based on the value of the two characters (e.g. 10 = A; 11 = B.......)

The problem I am having with the current regex is that it will replace all instances of the range in the string and I only need the first two characters to the left to be affected.

/10/A/A /11/B/A /12/C/A /13/D/A /14/E/A /15/F/A /16/G/A /17/H/A /18/I/A /19/J/A /20/K/A /21/L/A /22/M/A /23/N/A /24/O/A /25/P/A /26/Q/A /27/R/A /28/S/A /29/T/A /30/U/A /31/V/A /32/W/A /33/X/A /34/Y/A /35/Z/A

your question is not clear, post a before and after example.

I have a string of all numeric characters. The first two characters need to be translated into a single alpha character (e.g. 1023456789 = A23456789 & 20123456789 = K123456789.

The problem is that the PERL script I have set is greedy and is finding any pattern that I have set in the string and is not exclusively only the first two characters (e.g. 1023456789 = AN456789)

The conversion is to substitute the first two numeric characters from 10-35 with A-Z respectively (e.g. 10 = A; 11 = B; 35 = Z)

this is in PHP, you can convert to Perl easily

<?php

$letters=range('A','Z');
$numbers=range('10','35');
# make a mapping or hash in Perl, 'A'=>10 , 'B'=>11 etc
$map = array_combine($numbers,$letters) ;
$string1 = "1023456789";
$string2 = "20123456789";
echo "String 1 converted: " . $map[substr($string1,0,2)] . substr($string1,2) ."\n";
echo "String 2 converted: " . $map[substr($string2,0,2)] . substr($string2,2) ;
?>

For each line in your file,
split the string into an array
grab the first two array variables - do your math / substitution
merge it back into a string

thanks for the reply.

However, I am restricted to a one line script in the application I am working on. Not sure if this is possible?

How are you calling the script?

Well your regex was pretty long. I don't know why youre limited to one line, but how about a function?

sub_letter()
{
  echo "$1" | \
  awk 'BEGIN {split("0 0 0 0 0 0 0 0 0 A B C D E F G H I J K M N O P Q R S T U V W X Y Z", arr) }
       {print arr[substr($0,1,2)] substr($0,3)}'
}
while read record
do
   echo "before $record"
   echo "after  $(sub_letter $record)"
done < filename

filename just has several "strings" like 1020203456, the red part is all you need to have on your one-line. Your one line could also be an external process call

result=$(myscript.sh $before_string)

[/code]

here is the output:

before 102233455678
after  A2233455678
before 1122334050506
after  B22334050506
before 12707070707070
after  C707070707070
before 13505050505050
after  D505050505050
before 206660606060606
after  K6660606060606
before 30666060606060
after  V666060606060

You can do exactly the same in perl, create a dummy array with split, then get the first two chars and use them as an array offset. But it's way longer than one line. I did not have a modern version perl of perl at hand. Sorry.

How about something like this?

$ cat test.txt
1023456789
20123456789
12123456789
21123456789
35123456789

$ perl -ne'($num)=$_=~m/^(\d\d)/;print chr($num+55).substr($_,2)' test.txt
A23456789
K123456789
C123456789
L123456789
Z123456789

Dunno about the one-liner business, but how about something like

%num_to_char = { 10 => "A",  
                 11 => "B", 
                 12 => "C" 
# ...and so on -- maybe there is a better way of intiialising this
                  };
$s = "1020203456";  
$s =~ s/(^\d\d)/$num_to_char{$1}/;

similar to an earlier post:

perl -pi.bak -e 's/^(\d\d)/chr($1+55)/e' test.txt

but will edit the file inplace and create a backup of the original file

@num_to_char{(10..35)}=("A".."Z");