If shift appears by itself, then it removes the first element in the @_
array and returns it.  If an array is specified(i.e. shift @array), then it
uses that array.

-----Original Message-----
From: Steven M. Klass [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 12, 2002 1:20 PM
To: Brett W. McCoy
Cc: [EMAIL PROTECTED]
Subject: Re: Arrays 1x3 or 3x1 - The real questions


how does "shift" work?  In other words what if I do this

&Somefunction($var1, \@arry)

sub SomeFunction {
        my $var = $_[0]
        my $array = shift;
        foreach(@{$array}) {
          print "$_\n";
        }
 }

How does the shift operator know which is which?  I called it specifically 
earlier, because of this.  What am I missing?

Thanks so much

On Tuesday 12 February 2002 10:06 am, Brett W. McCoy wrote:
> On Tue, 12 Feb 2002, Steven M. Klass wrote:
> > Let's start off with some simple code..
> >
> > my $arg = &SomeFunction ( my @arry = qw/one two three/)
> >
> >
> >
> > sub SomeFunction {
> >     my @array = @_[0];
>
> No, you are only grabbing the first element of @_.  You should either pass
> the array as a reference (best way), or just grab up the entire @_.
> Keep in mind that if you pass an array and any scalars as arguments, they
> will all be flattened out into @_, as a single list.  This is why passing
> a reference is better, to differentiate lists and scalars.
>
> SomeFunction([qw(one two three)]);
>
> sub SomeFunction {
>       my $array = shift;
>       foreach(@{$array}) {
>         print "$_\n";
>       }
> }
>
> -- Brett
>                                           http://www.chapelperilous.net/
> ------------------------------------------------------------------------
> Removing the straw that broke the camel's back does not necessarily
> allow the camel to walk again.

-- 

 Steven M. Klass
 Physical Design Manager

 National Semiconductor Corp
 7400 W. Detroit Street
 Suite 170
 Chandler AZ 85226

 Ph:480-753-2503
 Fax:480-705-6407

 [EMAIL PROTECTED]
 http://www.nsc.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to