If you wanted to use shift to make an equivalent routine to Ex #1, you could
do it like this:

sub makeArray{
   while(shift @_){
      print $_."\n";
   }
}

That way if you called the sub like this...

   &makeArray('hello','world','!');

You should get this...

   hello
   world
   !

as your output.  'shift' removes the first element in an array and returns
the value.  If no variable is specified, the value is stored in the $_
variable.  I have lately been converting some of my code to use this format
instead of foreach() loops because it is shorter and easier to read.

-----Original Message-----
From: Jenda Krynicky
To: [EMAIL PROTECTED]
Sent: 2/3/02 1:23 PM
Subject: Re: why shift @_ ?

From:                   david wright <[EMAIL PROTECTED]>

> I have seen Ex #1 "corrected" (as being more well written) to Ex #2.
> In this case it is just being passed a $ but the data being passed was
> irrelevant. (though not a ref) I still don't see why, i guess i don't
> fully understand "shift". Any light shedder's appreciated, thanks : -)
> 
> EX #1:
> sub makeArray{
> my @array = @_;
>          foreach(@array){
>          print $_  . "\n";
>           }
>    }
> 
> 
> Ex #2
> sub makeArray{
> my @array = shift @_;
>          foreach(@array){
>          print $_  . "\n";
>           }
>    }
> 

The second example is nonsense. The

        my @array = shift @_;

will pull the first parameter and store it in ONE ELEMENT array 
@array. The other parameters will stay in @_.

Jenda

=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
                                        --- me

-- 
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