Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick one for "map {} @arr"

Status
Not open for further replies.

PinkeyNBrain

IS-IT--Management
Dec 12, 2006
279
US
I don't use the 'map' command much but though I'd give it try:

The following does what I want
Code:
$total = 0;
$total += $_ foreach (@arr_of_nums) ;

This didn't
Code:
$total = 0;
$total = map {$tot += $_} @arr_of_nums) ;

Essentially I'm left with
$total = $arr_of_nums[$#arr_of_nums]

Since the first version is working and the code I'm working on isn't sensitive to speed, I'll probably leave well enough alone. But still curious to know if the map command can be used this way. Guess I'm just out skin`in cats.
 
Typo - That second one should be (and is in my code)
Code:
$total = 0;
$total = map {$total += $_} @arr_of_nums) ;
 
Just do
Code:
$total=0;
map{$total+=$_}@arr_of_nums;
print$total;
perlfunc said:
map BLOCK LIST
map EXPR,LIST

Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation. In scalar context, returns the total number of elements so generated.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
OK - That makes sense. For my test, @arr_of_nums = (1,2,3,4,5,6,7,8,9) - Thanks
 
Yes, map{} is a list operator, so when you use it in scalar context you get a number instead of a list. Most (all?) of perls list operators exhibit the same behavior.

Francos example is an example of a construct that is frowned upon because it uses an operator that returns a value in void context.

The best way to do what you are trying is the "for" loop, it is also the fastest way.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevenADC - you don't often make typos, so I have to ask:

Did you mean '... "foreach" loop' ??
 
In Perl, "for" and "foreach" do exactly the same thing. Using the former saves you 4 letters in typing each time!
 
I did mean "foreach", but as Ishnid points out, there is actually no difference between "for" and "foreach". I think they use both for backwards compatibility with older code, but I am not sure.

BTW, Keven is the female genderization of Kevin. I assure you my hangy-down parts are between my legs and not on my chest. ;)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Read through my camel book (2nd ed) - not finding it saying anything about (for == foreach) == 1. Ran a couple of tests to convince myself and so far can't find any differences. However, like my compulsive ;'s, I'll likely continue to use 'foreach' under array contexts as it helps me in readability of my programs.

Thanks for the input though.
 
That enplanes this this guy

hehehe... yes, and it explains him as well. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 

The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. (Or because the Bourne shell is more familiar to you than csh, so writing for comes more naturally.)



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
foreach is a synonym of for, it's just a bit of syntactic sugar that makes your program a bit easier to read. I always use it too...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top