[oak perl] Applying shift() to an array reference
Adrien Lamothe
alamozzz at yahoo.com
Thu Sep 15 17:07:56 PDT 2005
Hi Steve,
They shouldn't expect anything else. The "shift"
operator works just like "pop" operator, only it works
on the items at the "beginning" (i.e. lowest index) of
the array. It sounds like they may really want to
simply index the array, as in:
$value = $AR[4];
Cheers,
Adrien
--- George Woolley <george at metaart.org> wrote:
> Hi Steve,
> An interesting question.
>
> My first thought is that's what I'd expect.
>
> What result are they expecting?
> George
>
> On Thursday 15 September 2005 12:58 am, steve
> kolupaev wrote:
> > Hi Everyone,
> >
> > An odd Perl question came up today on
> > my contract job in Washington state.
> >
> > When we call the shift function on an array
> > we get back the first element of the array,
> > and the array size drops by one.
> >
> > When we apply the shift function to a reference to
> the array, the same
> > thing happens.
> >
> > @AR = ( 1 .. 10 ); # create array of 10
> scalars
> >
> > shift @AR ; # the array is down to
> 9 scalars
> >
> > $ar = \@AR; # create a reference to
> the array
> >
> > shift @{$ar}; # the array is down to
> 8 scalars
> > shift @$ar; # the array is down to
> 7 scalars
> >
> > $AR[0] == 4 is true # the first element of
> @AR is now 4
> >
> > $ar->[0] == 4 is true # testing @AR through
> the reference $ar
> > # gives the same
> result, of course.
> >
> > Members of the project from a non-Perl background
> find this surprising.
> > But it is consistent with the current
> perlref.pod document and
> > matches my experience. Nothing protects the
> referent from a
> > properly expressed operation on its reference.
> >
> > Is this behavior common with to all major ports of
> Perl?
> >
> > Steve Kolupaev
> > Everett, Washington.
> >
> >
> >
> > _______________________________________________
> > Oakland mailing list
> > Oakland at pm.org
> > http://mail.pm.org/mailman/listinfo/oakland
>
> _______________________________________________
> Oakland mailing list
> Oakland at pm.org
> http://mail.pm.org/mailman/listinfo/oakland
>
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
More information about the Oakland
mailing list