Home: Perl Programming Help: Regular Expressions:
using a substitution with a map



PGScooter
stranger

Apr 5, 2009, 4:37 PM


Views: 9572
using a substitution with a map

Hi,

I have a list of items like this "one" "two" "three" in an array @countrylines. Note that the quotation marks are in the actual list items. I want to remove the quotation marks. I have two questions:

1. What is the quickest way to do this?
2. How can I do this by mapping a substitution?

I ended up doing the following:

Code
my @unquoted=map { /"(.*)"/ } @countrylines;


but I would like to know how to use a substitution within a map.

I tried to do the following:

Code
my @unquoted=map { s/"//g } @countrylines;

I do kind of understand why this doesn't work. I believe it's equivalent to doing something like this for a string:

Code
$unquoted = ($countrylines=~s/"/g);

which I think returns the number of substitutions made.

thanks!
The more you teach me, the more I learn. The more I learn, the more I teach.


KevinR
Veteran


Apr 6, 2009, 9:44 AM


Views: 9561
Re: [PGScooter] using a substitution with a map

Do you really need to assign the results to a new array? If not then this is the fastest way:


Code
@countrylines = ('"test"', '"test"'); 
tr/"//d for @countrylines;
print "@countrylines";


This works but will also change the original array:


Code
my @unquoted = map {tr/"//d; $_; } @countrylines;


If you really must assign to a new array this is probably best:


Code
my  @unquoted = @countrylines; 
tr/"//d for @unquoted;


Note the tr/// is not a regular expression, its an operator in the disguise of a regexp. So the "best" way is to not even use a regular expression.
-------------------------------------------------


PGScooter
stranger

Apr 6, 2009, 4:04 PM


Views: 9558
Re: [KevinR] using a substitution with a map

thanks Kevin!

It's always great to learn some new techniques and then go do some research on them. Awhile ago I would have just used a foreach loop but I am forcing myself to try to recognize when there is a better way to do something so I can try to learn more.
The more you teach me, the more I learn. The more I learn, the more I teach.


KevinR
Veteran


Apr 6, 2009, 5:12 PM


Views: 9557
Re: [PGScooter] using a substitution with a map

I really like the map function myself, it can make life pretty easy, but part of your question asked which is the fastest and using map probably is not the fastest. You can use map to remove the quotes, leave the original array unmodified and assign to a new array all in one shot:


Code
@new = map {my $t = $_; $t =~ tr/"//d; $t} @old;


but I so doubt that is faster or even as fast as the code I already posted.
-------------------------------------------------


PGScooter
stranger

Apr 7, 2009, 1:01 PM


Views: 9522
Re: [KevinR] using a substitution with a map

great!

I am also starting to gain respect for terse perl code. Sometimes it is harder for me to read and understand, but I like it a lot to use as study material.

Thanks!
The more you teach me, the more I learn. The more I learn, the more I teach.