Solved

perl s///

Posted on 2011-02-25
5
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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