PERL first two characters subsitutions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL first two characters subsitutions
# 1  
Old 09-30-2008
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
# 2  
Old 09-30-2008
your question is not clear, post a before and after example.
# 3  
Old 10-01-2008
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)
# 4  
Old 10-01-2008
this is in PHP, you can convert to Perl easily
Code:
<?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) ;
?>

# 5  
Old 10-01-2008
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
# 6  
Old 10-01-2008
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?
# 7  
Old 10-01-2008
How are you calling the script?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Pattern to remove words with less than 2 characters.

Hello. I've been thinking about how to go about this. I know I'm close but still does not work. I need to remove any word in that is not at least 2 characters long. I've removed all the non-alphabetic characters already (numbers included). Here's an example: my $string = "This string is a... (4 Replies)
Discussion started by: D2K
4 Replies

2. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

3. Shell Programming and Scripting

HTML Encoded characters -- Perl

Hello all I have a string like " Have Fun for the rest of the day !. I will meet you tomorrow!" ! is the HTML Equivalent of ! symbol. From the above string, i would like to remove only the HTML encoded special characters. Output should be like " Have Fun for the rest of the day... (4 Replies)
Discussion started by: vasuarjula
4 Replies

4. Shell Programming and Scripting

Help to fetch first two characters from a word in perl

Hi All, I have a word "DE_PR_Package__Basic" , i need to check if the first two characters of this words is DE or something else using perl script. Can anyone pls let me know how to proceed? Thanks in advance. Giri! (3 Replies)
Discussion started by: girish.raos
3 Replies

5. Shell Programming and Scripting

print last five characters with PERL regex

greetings citizens of Unix.com I am perplexed with an issue. The issue is trying to print the last 5 characters of a string in PERL. Below are demonstrated my daft attempts at performing the forementioned task. $row =~ m/^.*(.....)\s$/; $row =~ m/\w{5}\s*$/i;$row =~... (3 Replies)
Discussion started by: simply seth
3 Replies

6. Shell Programming and Scripting

Remove junk characters using Perl

Guys, can you help me in removing the junk character "^S" from the below line using perl Reference Data Not Recognised ^S Where a value is provided by the consuming system, which is not reco Thanks, M.Mohan (1 Reply)
Discussion started by: mohan_xunil
1 Replies

7. Shell Programming and Scripting

special characters handling in perl

Hi, Here is my piece of code-- sub per_user_qna_detail { for($index=0;$index<@records;$index++) { if($records =~ m/^(.*)\s*Morocco.*Entering\s*Module::authenticate/) { printf "INSIDE per_user_qna_detail on LINE NO $index\n"; $Time_Stamp = $1;... (0 Replies)
Discussion started by: namishtiwari
0 Replies

8. Shell Programming and Scripting

Want the substring of a string with special characters using perl

Hi, I am trying to separate the directory from the filename using the substring and it does not like it. $Complete_name="C:\ABCD\Reports_Split\090308.1630" I want $File_Name as 090308.1630 I tried using Substring function and it does not like slashes in the complete_name. Any... (5 Replies)
Discussion started by: sushma0907
5 Replies

9. Shell Programming and Scripting

Reading a file having junk characters in perl

Can anyone tell me how to read a file in perl having junk characters . I have only one junk character which is repeated many times in the file. While i'm reading and printing the file , it is displaying till the 1st occurence of that junk character and rest of the file is not being read. (1 Reply)
Discussion started by: k_surya
1 Replies

10. UNIX for Dummies Questions & Answers

Exceed subsitutions?

Does anyone know if there any application perform better than Exceed? It seems some video card doesn't support Exceed. thx (1 Reply)
Discussion started by: E-Quality
1 Replies
Login or Register to Ask a Question