Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

What array negative number counting means

by Anonymous Monk
on Jul 27, 2020 at 23:35 UTC ( #11119867=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Suppose we have

 my @x = ("foo", "bar", "light");

[0] is foo, why $x[-y] is equivalent to $x[(scalar @x) - y] ? I don't get this $x[(scalar @x) - y] expression, please enlighten me monks.

Replies are listed 'Best First'.
Re: What array negative number counting means
by syphilis (Bishop) on Jul 27, 2020 at 23:46 UTC
    If you want to access the last element of an array, it's fewer keystrokes to do $x[-1], or $x[-2] for second last, etc.
    It's also clearer, IMO.
    But if you prefer to do $x[(scalar @x) - y], then that's ok.
    That's about it ... TIMTOWTDI.

    Cheers,
    Rob
      Could you please explain to me what $x[(scalar @x) - y] is doing?
        (scalar @x) is the length of @x here 3

        (scalar @x) - 1 == 2

        In other words

        $x[-y] is a shortcut for $x[(scalar @x) - y]

        =>

        $x[-1] eq $x[2] eq "light"

        $x[-2] eq $x[1] eq "bar"

        $x[-3] eq $x[0] eq "foo"

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: What array negative number counting means
by AnomalousMonk (Bishop) on Jul 28, 2020 at 03:06 UTC

    As an historical aside, note that using the result of evaluating the expression  (scalar @ra) - 1 is not quite the same as using an index of -1 or $#array if you have set the array base $[ (which defaults to 0) to be 1 — but don't do that! See $[ in Deprecated and removed variables in perlvar. Assignment to $[ was first deprecated in Perl version 5.12 and has been increasingly constrained in subsequent versions.

    c:\@Work\Perl\monks>perl -wMstrict -le "$[ = 1; ;; my @ra = qw(zero one two three); ;; print scalar @ra; print 0 + @ra; print $ra[ (scalar @ra) - 1 ]; print $ra[ @ra - 1 ]; print $ra[ @ra ]; print $ra[ -1 ]; print $ra[ $#ra ]; " Use of assignment to $[ is deprecated at -e line 1. 4 4 two two three three three
    In all cases (and regardless of the array base — but again, don't futz with that):
    • evaluating an array in any scalar context yields the number of elements (or length) of the array;
    • $#array yields the maximum index of the array;
    • negative indices are handled consistently.


    Give a man a fish:  <%-{-{-{-<

      Thanks for the clarification.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11119867]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2020-08-15 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (78 votes). Check out past polls.

    Notices?