Solved

perl s///

Posted on 2011-02-25
5
226 Views
Last Modified: 2012-05-11
I have an string that looks like $data = r.r.123.123 I also have a corisponding  hash with %hash{r.r.) = 12.12. I need to match the r.r. in $data with the %hash{key} and get the value and replace the r.r. in data with that string which becomes 12.12.123.123. I am using perls s///.  This is the statement

$data =~ s/([G|R|B|O|Y|0-9]{1,3}\.[G|R|B|O|Y|0-9]{1,3}\.)/$hash{$1}/i;

The match is made in the first 1/2 of the search but when I try to get the value from the $hash{$1} I do not get anything.  I can print the $1 and see the key r.r. I tried replacing $hash{$1} with $hash{'$1'} that did not work. Also I tried using the eval statement

eval "\$data =~ s/([G|R|B|O|Y|0-9]{1,3}\.[G|R|B|O|Y|0-9]{1,3}\.)/$hash{$1}/i";

which did not work either. Can someone please help me?
0
Comment
Question by:dduffyi
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34981130
I don't think you need to be so complicated.  Just search for '.'-terminated substrings.

%hash = ('r.r.', '12.12.');
$data = 'r.r.123.123';

$data =~ s/([^\.]+\.[^\.]+\.)/$hash{$1}/i;

print "$data\n";

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34981206
FYI - Your version works for me, too.  Not sure what your problem was (is?).

%hash = ('r.r.', '12.12.');
$data = 'r.r.123.123';

$data =~ s/([G|R|B|O|Y|0-9]{1,3}\.[G|R|B|O|Y|0-9]{1,3}\.)/$hash{$1}/i;

print "$data\n";

Open in new window


c:\temp>foo.pl
12.12.123.123

Open in new window

0
 

Author Comment

by:dduffyi
ID: 34981396
Thank you very much for your quick response. I think I might have blanks after the (r.r.) in my hash. I read the keys/values from a file into the hash. I will try to remove anything following the last period and see if I can make it work, but I would not have noticed the blanks if not for you testing the same code. Once again thanks and I will let you know if that is what the problem is.
0
 

Author Closing Comment

by:dduffyi
ID: 34981481
Thank you it was the initial hash. I really appreciate the help. I was not even looking at the hash itself. You saved me a lot of time. :)
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34981525
Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert grep lines to perl 6 37
perl: Cleaning meta tags using RegEX 12 75
SUM 2 INTEGER ARRAYS INTO 1 10 100
Apps blocked by Java 9 79
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question