Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I totally agree with you, I think that it is backwards that it doesn't DWIM. The other comments are just the typical lame gut reaction by people who don't get it comments.
    • I don't think it's a gut reaction, lame or otherwise. The proponents seem to be the ones who haven't thought things through. It's not at all clear what DWIM behavior is in this case. It has to handle nonempty arrays as well:

      my @foo = 1 .. 3;
      $foo[10] = "foo"; # legal
      $foo[-10] = "foo"; # illegal, but what do you expect?
      • my @foo = 1 .. 3;
        $foo[10] = "foo"; # legal
        $foo[-10] = "foo"; # illegal, but what do you expect?

        For brevity's sake, let's cut the index down to 5. So, using your example, $foo[5] = "foo" would be:


        So, what would I expect from $foo[-5] = "foo"? I would expect @foo to be:

        # One less undef, because the reverse index starts at -1, not -0.

        • Thanks. So you expect it to move the existing array elements into other places. Is there anyone else out there besides you and sky who would expect that?

          I think the reason that assigning to nonexistent array elements with negative indices doesn't work is that there's no consistent way to do it.

          I can only guess that it seems consistent to you because, from the point of view of someone who only uses negative array indices, the equivalent assignment with a positive index is "moving" array elements. But I don
          • Is there anyone else out there besides you and sky who would expect that?

            Not normally, but in Perl? Yes, I would.

            Here is why...

            Suppose you have an array, of any size. When you hit it with an assignment using an index who's absolute value is larger than the array size... it will, basically perform a "push" onto the end of the array (the right side) until the array size = the absolute value of the index if the index is positive.

            Then it will do the assignment.

            So, it is a two step process for positive index values... grow the array to the needed size... then make the assignment.

            With this in mind, I would expect Perl to grow the array from the other direction (like an "unshift") for index values that are negative.

            The sign of the index value would determine which end grows, the absolute value would determine the amount of growth. (This is for situations where the absolute value of the index is larger than the size of the array.)

            I would consider that to be "consistent". The fact that it doesn't do this, however... is not surprising. Because I'm not aware of any other language that does it...

            But, I do think that would be the "right thing" and the "do what I mean thing" for the situation.