Re: Shift Question
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Tue, 27 Sep 2005 15:24:49 -0700
Mulander wrote:
> If I understood you question properly you want to know why people use
> shift in subrutines and how does shift work.
>
> I will try to make it short:
> shift works on lists, it removes the first element of the list
perldoc -q "What is the difference between a list and an array"
You can't use shift() on a list, only on an array:
$ perl -le'my $x = shift qw(10 11 12 13 14)'
Type of arg 1 to shift must be array (not list) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
> and returns it as a lvalue
The term 'lvalue' has a specific meaning in Perl (and CS) which usually means
something that can be assigned to. Functions like substr() and vec() are
lvalue subs while shift() is not.
$ perl -le'my $x = "abc"; substr( $x, 2 ) = "xyz";'
$ perl -le'my @x = qw"a b c"; shift( @x ) = "xyz";'
Can't modify shift in scalar assignment at -e line 1, near ""xyz";"
Execution of -e aborted due to compilation errors.
>
> [snip]
>
> Hope this will clear some things up, you can check also:
> perldoc -f shift
> perldoc -f unshift
> perldoc -f pop
> perldoc -f push
And also:
perldoc -f splice
perldoc perlsub
John
--
use Perl;
program
fulfillment
.
- References:
- RE: Shift Question
- From: Ryan Frantz
- Shift Question
- From: Mulander
- RE: Shift Question
- Prev by Date: Re: Shift Question
- Next by Date: Re: Shift Question
- Previous by thread: Re: Shift Question
- Next by thread: Block Confusion
- Index(es):
Relevant Pages
|
|