Perl regular expression

Hi ,

I have the below array

my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43");

When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below

my @action = grep { $_ =~ s/delivered to governor on (.*)|ram/$2/ } @actionText;
print "@action"."\n";

the output is
--------------

21/23/3345 2345/43

how is this 2345/43 getting stored in $action[1] array as i have not used () after ram in regular expression...

Please help me to understand this

Thanks
Ragilla

I'm not terribly familiar with perl, but assuming the s/... works like normal sed: I would guess it's matching the entire 'ram' element @actiontext[2], but you're only replacing the 'ram' part with $2 (or actually nothing, for that match) - leaving the remainder of that element to be put into @action[1].

Hi,

Here you are grepping the array element which contain string 'delivered to governor on' or 'ram' and do some substitution. So you wont get the array elements which dont have the matching string.
I think, you need all the array elements and do the substitition in some of the array elements.

my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43");
my @action = map{$_=~ s/delivered to governor on |ram//;$_;} @actionText;
print "@action\n";

This is what you are looking for ..?
It will remove the string 'delivered to governor on ' or 'ram' before assigning all the array elements from @actionText to @action.

Cheers,
Ranga :slight_smile:

Hi Ranga,

you misunderstood my question, i know the difference between map and grep.

i wanted to substitute "delivered to governor on 21/23/3345" with 21/23/3354 and "ram 2345/43" with "2345/43", so i substituted with $2 in delivered to governor on 21/23/3345, but the string when matched ram... how it is substituting "2345/43" even though i dint group that in regular expression.

Regards
Sridhar

Your question should be - why is "21/23/3345" showing up in the array @action.
The string " 2345/43" has all the more reason to be there because you substituted "ram" by nothing, thereby chopping it off.

tyler_durden

1 Like

You are chopping the texts 'delivered to governor on ' or 'ram ' with $2. Here $2 deosn't have any value hence it substitute with empty string.
durden_tyler has been stated above clearly. Thanks mate :slight_smile:

What you are trying to do with this..?

Cheers,
Ranga :slight_smile:

Hi tyler_durden,

Thanks for your reply, i missed that.
I have one more question. i have code like below , i need to get the text between certain patterns , suppose i have below array

use strict;
use warnings;
@a = ("my asdf bye", "ram lslkdjfsalf raj");

@a = grep { $_ =~ s/my (.*) bye|ram (.*) raj/$1$2/ } @a;

as first pattern match the required text will be in $1 and As second pattern match the required text will be in $2.

but when first pattern match $2 is empty it will give warning like this
Use of uninitialized value $2 in concatenation (.) or string at

So how to avoid those warnings .. please help me..

thanks in advance
Ragilla

Can you post a specific example in which you encounter that error message?

I tried fiddling with the example you posted but can't seem to figure out what you are trying to do.

$
$ perl -le '@a=("my asdf bye","ram lslkdjfsalf raj"); @a=grep{$_ =~ s/my (.*) bye|ram (.*) raj/$1$2/}@a; print join",", @a'
asdf,lslkdjfsalf
$
$
$
$ perl -le '@a=("my bye","ram lslkdjfsalf raj"); @a=grep{$_ =~ s/my (.*) bye|ram (.*) raj/$1$2/}@a; print join",", @a'
lslkdjfsalf
$
$
$ perl -le '@a=("my asdf bye","ram raj"); @a=grep{$_ =~ s/my (.*) bye|ram (.*) raj/$1$2/}@a; print join",", @a'
asdf
$
$

Also, what is the version of Perl you are using? Paste the output of -

perl -v

tyler_durden