Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

array index -1 oddity

by shemp (Deacon)
on Dec 06, 2002 at 18:30 UTC ( #218121=perlquestion: print w/replies, xml ) Need Help??

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

i noticed something about using array index -1 today, that seems odd. in the following code, using index -1 didnt function quite as i expected.
my @blah; print $blah[-1] . "\n"; $blah[-1] = "yes"; print $blah[-1] . "\n";
Printing index -1 works fine, but trying to assign to index -1 results in the error:
Modification of non-creatable array value attempted, subscript -1 at ...

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 dont necessarily call this a bug, just unexpected.

Replies are listed 'Best First'.
•Re: array index -1 oddity
by merlyn (Sage) on Dec 06, 2002 at 18:33 UTC
    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.

      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.

(tye)Re: array index -1 oddity
by tye (Sage) on Dec 07, 2002 at 06:14 UTC

    Perhaps it would help to consider a related situation.

    I can almost see it making some sense to have $blah[-1]= $x; create a last element (a form of autovivification) and then set it. But if that were true than what should $blah[-2]= $y; do? Should it create a second-to-last element? So if @blah only contains one element, it would be the same as unshift @blah, $y; ? And if @blah is empty, it would be the same as @blah= ( $y, undef ); ?

    Going to such extremes of autovivification stretches the imagination a bit more and is harder to justify/swallow. But I can't see drawing the line between -1 and -2 (or between any other particular pair of negative numbers) so either we'd always autovivify or never. Things get more bizarre when you consider all of the cases for $blah[-3]= $z;.

    So, if you like, you could write up a patch that causes such assignments to unshift undef onto the array until the index makes sense. It kinda makes sense and even seems rather Perlish. I'm more looking for the option of having less autovivification than more, but some might like the idea. There's even a chance your patch might get accepted. I'm not sure how big of a chance...

            - tye
Re: array index -1 oddity
by shotgunefx (Parson) on Dec 06, 2002 at 22:08 UTC
    You could write that like so...
    $blah[++$#blah] = 'New element';

    You should rarely if NEVER play with $[
    Most code assumes $[ is 0 so your likely to get some very subtle bugs.

    -Lee

    "To be civilized is to deny one's nature."
Re: array index -1 oddity
by Aristotle (Chancellor) on Dec 07, 2002 at 13:04 UTC
    If you really do need something like that (I'd think about that first), you can write it briefly as $arr[@arr ? $#arr : 0] = $foo; or maybe (@arr ? $arr[-1] : $arr[0]) = $foo;

    Makeshifts last the longest.

Log In?
Username:
Password:

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

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















    Results (150 votes). Check out past polls.

    Notices?