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: Beginner:
MAP function and its affect....

 



yanivr78
Novice

Mar 15, 2013, 2:57 AM

Post #1 of 6 (464 views)
MAP function and its affect.... Can't Post

Hi all,

I have two lists, @a and @b.
after running a function on @b using the below :


Code
#!/usr/bin/perl 

# MAP = returns a list of Regexp strings(similar to grep)

@a = (1,2,3,1.2);
@b = map($_ *= 2,@a); # first add *2 to each number from @a

print "\n@a\n";
print "\n@b\n";


I print the lists, and I've found that @a was changed as well from some reason.


In Reply To
@a:
2 4 6 2.4

@b:
2 4 6 2.4


Can you explain why ?


(This post was edited by yanivr78 on Mar 15, 2013, 2:59 AM)


winfinit
User

Mar 15, 2013, 5:14 AM

Post #2 of 6 (461 views)
Re: [yanivr78] MAP function and its affect.... [In reply to] Can't Post

that is because you are changing in your map function values of @a array.

a *= b is the same as a = a*b, and in your case $_ is a current element of an array, and by doing $_ *= 2, you are modifying it.

if you want to iterate through elements of an array "a" and multiply each by two, and then assign those values to array "b" you just need to remove "="

@b = map($_ * 2,@a);


Laurent_R
Enthusiast / Moderator

Mar 15, 2013, 11:51 AM

Post #3 of 6 (451 views)
Re: [yanivr78] MAP function and its affect.... [In reply to] Can't Post

Just to add one point, although the previous answer was pretty clear. In:


Code
@b = map($_ *= 2,@a);


$_ is not a copy of the elements of the @a array, but an alias for each such element. So that if you modify $_, you actually modify the original array.

And BTW, you code does not add 2 to each number of the array, but multiplies each number by 2.


yanivr78
Novice

Mar 16, 2013, 2:06 PM

Post #4 of 6 (440 views)
Re: [Laurent_R] MAP function and its affect.... [In reply to] Can't Post

Got it !
Thanks guys.

BTW,
I would like to read about $_, how it works...what it does, whats the name of it and how to use it.

do you know about a specific reference for that ?


Chris Charley
User

Mar 16, 2013, 3:28 PM

Post #5 of 6 (438 views)
Re: [yanivr78] MAP function and its affect.... [In reply to] Can't Post

Special variables


Laurent_R
Enthusiast / Moderator

Mar 16, 2013, 4:22 PM

Post #6 of 6 (433 views)
Re: [Chris Charley] MAP function and its affect.... [In reply to] Can't Post

Chris gave you a pointer to what is most probably the best document on special variables and, especially, on $_.

Yet, in a number of cases, additional information can be found on what you are looking for in the documentation about other items, such as for example the grep, for, foreach or map functions.

Don't hesitate to ask if some behaviors related $_ are not obvious to you.

 
 


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

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