Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

perl s///

Posted on 2011-02-25
5
Medium Priority
?
231 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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 …
Starting up a Project

636 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