Shift Question



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
.



Relevant Pages

  • Re: Shift Question
    ... > shift in subrutines and how does shift work. ... > shift works on lists, it removes the first element of the list ... perldoc -q "What is the difference between a list and an array" ... The term 'lvalue' has a specific meaning in Perl which usually means ...
    (perl.beginners)
  • Re: Return subset to drop down box
    ... I have a workbook to track production. ... input which shift they are inputting data for via drop ... employees who are on second shift to another drop down box on the ...
    (microsoft.public.excel)
  • Re: Shift Question
    ... > shift in subrutines and how does shift work. ... > shift works on lists, it removes the first element of the list (the 0 ... > perldoc -f unshift ... Additionally a very common idiom and where you may be seeing this so ...
    (perl.beginners)
  • Re: difference between "@_" and "shift"
    ... shift is a function for pulling off the first element from a list. ... You can push to and pop from the end ... of lists to treat them as stacks; you can shift from and unshift to the ...
    (perl.beginners)
  • Re: Automatic Capitalization
    ... >> You will have to train your users to use their Shift keys. ... >>> We have some users who are creating numbered lists, and are doing a bit of ... >>> Capitalize the first letter of sentences is turned on in AutoCorrect ... To paraphrase what Wilma mentioned in the thread "Re: numeric list question" ...
    (microsoft.public.word.newusers)