Perl Monk, Perl Meditation PerlMonks

### Re: @array elements multiplication with another array elements.

by Don Coyote (Pilgrim)
 on Nov 03, 2012 at 15:57 UTC ( #1002112=note: print w/replies, xml ) Need Help??

Point to note, the for loop uses the construct of scalar(@arr) and the map operation uses the special variable \$# (+ arrayname) \$#arr to determine the length of the list being implemented within the operations.

where @arr = (1,2,3,4)

scalar(@arr) returns the number of items in the list scalar(@arr) = 4

\$#arr returns a zero indexed length of items list \$#arr = 3

\$#arr returns the highest index of the array \$#arr = 3

The C style for loop has the condition of the increment being lower than scalar(@arr) where;

scalar(@arr) = 4, ( \$i = 0, \$i < scalar(@arr), \$i++ ) = 0,1,2,3

The perl map operation however, inserts the incremented counter for the \$_ variable where;

\$#arr = 3, 0..\$#arr = 0,1,2,3

updates to definition of \$# variable as result of reply from Anomolous.

Should the \$ARRAY_BASE special variable \$[ be set to other than the default of 0. The \$#arr may return an offset to the zero indexed result that you are probably not expecting. Generally use of this variable is frowned upon at best. However.. dah dih dah dih.

my @arr = split //,'Coyote'; print map {\$arr[\$_]} 0..\$#arr;

Replies are listed 'Best First'.
Re^2: @array elements multiplication with another array elements.
by AnomalousMonk (Chancellor) on Nov 03, 2012 at 16:20 UTC
\$#arr returns a zero indexed length of items list \$#arr = 3

I think it is fundamentally misleading to refer to what  \$#array returns as a "length". Rather, I would say it is the highest index of the array, and depends on what the \$ARRAY_BASE special variable (aka \$[ – see perlvar) has been set to (but don't do that!).

Thanks Anomolous, point noted. I should also reflect that it is not truly the map operator that is using the \$#arr construct to obtain the highest array index as such, rather it is being used by the .. range operator. As map expects a list of some description to work with after the modifiying operation. In this case we give the list in scalar context of a range, much like the for iterator, so perl goes ah yah, this is some funky for loop right, and cooly proceeds.

I will now look into how the \$ARRAY_BASE special variable affects the \$# variable. I would not have thought a difference would be made, being as the highest index of an array is always going to be the highest index? (unless \$[ is set higher perhaps... hmm...).

*Footsteps echo quietly away as Coyote trundles off into the dark echoing corridors of the echosome vaults.

I will now look into how the \$ARRAY_BASE special variable affects the \$# variable.

By all means investigate the-special-variable-whose-name-must-not-be-written to learn its heterodoxy, but never use this impious and odious monstrosity lest your script be shunned throughout all lands and ages, and your name be cursed by maintainers.

>perl -wMstrict -le "\$[ = 654; my @ra = 0 .. 12; print \$#ra; " Use of assignment to \$[ is deprecated at -e line 1. 666

Create A New User
Node Status?
node history
Node Type: note [id://1002112]
help
Chatterbox?
 [Discipulus]: choroba i made you Bishop !! [choroba]: 9999 points until level 23 - Archbishop. [LanX]: darn trying to downvote him again fails ... ;-P [Discipulus]: congrats! [LanX]: His Excelleny Choroba will now nbe clothed in his new robe ... [Discipulus]: bishop ship sheeps in the shop.. [1nickt]: Discipulus too easy! [1nickt]: "The sixth sheik's sixth sheep's sick" (if you are after 'sh' tongue twisters) [Discipulus]: ok try it in Eatalian ;=) [Eily]: Discipulus I don't suppose you can do the same for me can you ? :P

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-09-26 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (294 votes). Check out past polls.

Notices?