Shift Question
- From: netprobe@xxxxxxxxx (Mulander)
- Date: Tue, 27 Sep 2005 23:47:26 +0200
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 ( the 0
indexed element ) and returns it as a lvalue ( if there are no more
elements in a list it returns undef ).
Here is an example:
my @list = qw(a b c d);
print shift @list,"\n";
print "my list is now @list\n";
print shift @list,"\n";
print "my list is now @list\n";
print shift @list,"\n";
print "my list is now @list\n";
print shift @list,"\n",
print "my list is now @list\n";
this should print something like this ( althoug I did not have the
time to test it )
a
my list is now b c d
b
my list is now c d
c
and so on...
If you use shift without giving it the list name to work on it will
refer to @_ or @ARGV ( it is decided upon the file scope ). So when
you define a sub like this:
sub somesub {
my $arg1 = shift;
}
You did something simmilar to my $arg1 = $_[0]; but more elegant ( in
my opinion ) and you removed the first element from the arguments list
( witch is quite usefull ).
Hope this will clear some things up, you can check also:
perldoc -f shift
perldoc -f unshift
perldoc -f pop
perldoc -f push
.
- Follow-Ups:
- Re: Shift Question
- From: John W. Krahn
- Re: Shift Question
- From: Wiggins d'Anconia
- Re: Shift Question
- References:
- RE: Shift Question
- From: Ryan Frantz
- RE: Shift Question
- Prev by Date: RE: Shift Question
- Next by Date: Re: Shift Question
- Previous by thread: RE: Shift Question
- Next by thread: Re: Shift Question
- Index(es):
Relevant Pages
|