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: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Map function

 



Cure
User

Jul 6, 2000, 7:11 AM

Post #1 of 7 (46483 views)
Map function Can't Post

@new = map /o/ ? $& : (), qw!one two three four five!;

Whats the value of @new; Explain how the array @new obtained its new values.


Cure


Jasmine
Administrator

Jul 5, 2000, 10:08 PM

Post #2 of 7 (46481 views)
Re: Map function [In reply to] Can't Post

I agree with SixKiller (is it fair if I play, too? Smile)

@new = map /o/ ? $& : (), qw!one two three four five!;

It's basically saying that if o matches, as it does in (one, two, and four), toss only the precise text that matched (stored in $&), which is o, into @new. If it doesn't match, it's nothing ()

However, if Cure had used:

@new = map /o/ ? $_ : (), qw!one two three four five!;

then it would have yielded the results that mckhendry posted.

Performance note: Because Perl needs to know if the exact matched data will be needed, it looks for $& at compile time. If it finds $& anywhere in the program (and even in libraries and modules that the program uses), it will take the time to store the matched data for each regex in the program, even in regexes that do not use $&. This will slow down all regexes, so it's a good rule of thumb not to use $& unless you really, really have to, especially in a library or module.

This is fun Smile


mckhendry
Deleted

Jul 6, 2000, 7:39 AM

Post #3 of 7 (46481 views)
Re: Map function [In reply to] Can't Post

(one, two, four)

map evaluates the list of words with the regular expression, and returns either what was matched or an empty list. Because it evaluates the block in list context you get the list of three words instead of just the last word that matched. The ? : is the ternary operator and $& is what was matched.

I'm pretty sure I'm right, but if I'm not this is going to be really embarrassing.


SixKiller
Deleted

Jul 6, 2000, 9:04 AM

Post #4 of 7 (46481 views)
Re: Map function [In reply to] Can't Post

@new will be a list ('o','o','o').
That's because there are three elements that match /o/ so $& returned 3 times...all the others one doesn't match and an empty list is returned. $& is 'o' because this is the last matched regex...sorry for my english..i hope you can understand what i'm trying to say


errr
Deleted

Jul 6, 2000, 6:06 PM

Post #5 of 7 (46481 views)
Re: Map function [In reply to] Can't Post

Well I'm going to congrat everyone for not plugging this into perl like they did the last quiz...

its the triple 'o' for the reasons given
I'm sure jasmine, that you also realize map /o/ ? $_ : () is just a fancy way of writing grep :P
anyways, the thing about $& being slow IS true.. but in later versions of perl the performance hit is a LOT less than it has been in the past.
if, like me, you don't care about performance and like readability instead, do a
use English; and then you can refer to $& as $MATCH


Cure
User

Jul 13, 2000, 4:31 PM

Post #6 of 7 (46481 views)
Re: Map function [In reply to] Can't Post

@new = map /o/ ? $& : (), qw!one two three four five!;

THE ANSWER-> There are three elements in the array that contain the character o, so $& will return o 3 times. The others elements dont have the character o, so the empty list will be returned instead of o. So the value of @new will be ('o','o','o').

Good job jasmine, Sixkiller and errr


Cure


hydpm
User

Jul 17, 2007, 2:21 AM

Post #7 of 7 (44363 views)
Re: [Cure] Map function [In reply to] Can't Post

[nagaraju@kingfisher ~]$ cat maptest.pl

@new = map /o/ ? $& : (), qw!one two three four five!;
foreach (@new) {
print $_,"\n";
}
[nagaraju@kingfisher ~]$ perl maptest.pl
o
o
o
[nagaraju@kingfisher ~]$

 
 


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

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