?
Solved

Perl - Regular Expression - List

Posted on 2003-03-13
5
Medium Priority
?
205 Views
Last Modified: 2010-03-05
Hi friends

I've a list of text which should be matched, and then get replaced by another list of text.  For eg., the first list will be "0,1,2,3" and the second list would be "Zero,One,Two,Three".  Is there any SINGLE pattern with which I can do this?  If so please tell me.

E.g:
Input:  This is 0 test and 1 test.
Output: This is Zero test and One test.

Thanks
Srik
0
Comment
Question by:vi_srikanth
5 Comments
 
LVL 1

Expert Comment

by:zerofill
ID: 8134512
Here is an example code.
Let's say you have the two lists stored in two arrays - @first and @second

@first=("0", "1", "2");
@second=("zero", "one", "two");

$string="This is 0 test and 1 test.";
$counter=0;
foreach $element (@first)
{
$string=~ s/$first[$counter]/$second[$counter]/g;
$counter++;
}
print "$string"; #will print This is Zero test and One test.

NOTE: you must ensure that @first and @second have equal number of elements in order this to work

Good Luck
0
 
LVL 85

Accepted Solution

by:
ozo earned 80 total points
ID: 8134974
@first=(0..2);
@second=qw(zero one two);
@sub{@first}=@second;
$re=join'|',map quotemeta,@first;
$string = "This is 0 test and 1 test.";
$string =~ s/($re)/$sub{$1}/g;

0
 
LVL 4

Author Comment

by:vi_srikanth
ID: 8135300
Hi OZO,

It works precisely.  Thanks a lot.  I was looking for such a code ONLY.

But, I'm unable to figure out the meaning for the code-part:

@sub{@first}=@second;

"sub" is a hash array, but you've used "@sub" instead of "%sub".  Also, if I try the same code by changing @sub to %sub, I'm getting a compiler error.  Could u please help me?

Thanks
Srik
0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 8136414
@sub{@first} = @second

says to assign multiple values (@second) to multiple keys (@first) in the hash %sub.  My guess is that the @sub{} syntax was chosen to indicate that it is acting as a list and can be assigned to as such where %sub usually indicates hash "mode" where you must assign key => value pairs to it.
0
 
LVL 4

Author Comment

by:vi_srikanth
ID: 8141171
Thank You friends.

It is really weird & odd to have @sub to represent a hash array!!!  I'm really finding difficult to digest this.  Thanks again.

Bye
Srik
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

621 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