Solved

perl s///

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

760 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

16 Experts available now in Live!

Get 1:1 Help Now