CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



PGScooter
stranger

Apr 5, 2009, 4:37 PM

Post #1 of 5 (2688 views)
using a substitution with a map Can't Post

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

Post #2 of 5 (2677 views)
Re: [PGScooter] using a substitution with a map [In reply to] Can't Post

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

Post #3 of 5 (2674 views)
Re: [KevinR] using a substitution with a map [In reply to] Can't Post

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

Post #4 of 5 (2673 views)
Re: [PGScooter] using a substitution with a map [In reply to] Can't Post

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

Post #5 of 5 (2638 views)
Re: [KevinR] using a substitution with a map [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives