Solved

perl s///

Posted on 2011-02-25
5
225 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now