develooper Front page | perl.perl5.porters | Postings from January 2014

[perl #120947] Array changes size when accessing past the end using for or grep

Thread Next
From:
James E Keenan via RT
Date:
January 8, 2014 00:29
Subject:
[perl #120947] Array changes size when accessing past the end using for or grep
Message ID:
rt-4.0.18-12892-1389140966-415.120947-15-0@perl.org
On Tue Jan 07 15:51:51 2014, jkeenan wrote:
> On Tue Jan 07 15:38:06 2014, jkeenan wrote:
> > On Tue Jan 07 10:40:31 2014, chris_jack@msn.com wrote:
> > > This is a bug report for perl from chris_jack@msn.com,generated
> > > with
> > > the help of perlbug 1.39 running under perl 5.14.4.
> > >
> > > -----------------------------------------------------------------[Please
> > > describe your issue here]
> > > # The below perl script bug.pl walks through the issue# I get the
> > > output:Original length:    2Length is the same: 2Length is more:
> > > 4Original length:    2Length is more:     4
> > > when I would expect the output:Original length:    2Length is the
> > > same: 2Length is more:     2Original length:    2Length is more:
> > > 2
> > > Basically both for and grep seem to extend an array simply by
> > > lookingat the contents of elements past the end of the array
> > > however
> > > thisdoes not happen if I use an element past the end of the array
> > > in
> > > astraight scalar context.--start bug.pl--# We set up an array of
> > > length 2my @array = (1, 2);
> > > # Yes: it really is length 2print "Original length:    " .
> > > scalar(@array) . "\n";
> > > # When we access an element past the end as followsunless
> > > (defined($array[3])) {}
> > > # Yes: it is still length 2print "Length is the same: " .
> > > scalar(@array) . "\n";
> > > # When we access an element past the end using for as followsfor my
> > > $a
> > > ($array[3]) {}
> > > # Suddenly the length has shot up to 4: is this a bug?print "Length
> > > is
> > > more:     " . scalar(@array) . "\n";
> > > # Reset the arraymy @array = (1, 2);
> > > # Yes: it is length 2 againprint "Original length:    " .
> > > scalar(@array) . "\n";
> > > # When we access an element past the end using grep as followsgrep
> > > {defined} ($array[3]);
> > > # Again the length has shot up to 4: is this a bug?print "Length is
> > > more:     " . scalar(@array) . "\n";--end bug.pl--
> > >
> >

There is no bug in Perl here.  As Programming Perl (4th ed., page 10) states:

#####
"If you want to assign to one array element at a time, you can; the elements of the array are automatically created as needed."
#####

So if you request $array[3], Perl will automatically expand the size of the array as needed, filling in all "in-between" elements with the undefined value.  In both cases in your sample program, the arrays have expanded because you have requested that they do so.

Marking ticket resolved.

Thank you very much.
Jim Keenan

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=120947

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About