Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

•Re: array index -1 oddity

by merlyn (Sage)
on Dec 06, 2002 at 18:33 UTC ( #218122=note: print w/replies, xml ) Need Help??


in reply to array index -1 oddity

I would have thought that trying to assign to the -1 element in an uninitialized array would assign to element 0, but i guess not.
I would call that unexpected.

Assigning to element -1 assigns to the highest element in the array. You have an empty array. There is no "highest element". That's out of range.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: array index -1 oddity
by scain (Curate) on Dec 06, 2002 at 19:07 UTC
    Really? That strikes me as a little odd and counter-intuitive. There are instances where having an array index of -1 (or -2 or -3) are quite useful. In particular, some numerical methods come to mind. I suppose that if I felt like doing that, I would need to modify a magic variable first? Like $[ if I read man perlvar correctly.

    Scott
    Project coordinator of the Generic Model Organism Database Project

      Maybe you missed this statement in perldata:
        Variable names
          Perl has three built-in data types: scalars, arrays of scalars, and
          associative arrays of scalars, known as "hashes". Normal arrays are
          ordered lists of scalars indexed by number, starting with 0 and with
          negative subscripts counting from the end. Hashes are unordered
          collections of scalar values indexed by their associated string key.
      

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://218122]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2020-05-30 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?