Front page | perl.beginners |
Postings from February 2002
Re: Arrays 1x3 or 3x1 - The real questions
Thread Previous
From:
Steven M. Klass
Date:
February 12, 2002 14:25
Subject:
Re: Arrays 1x3 or 3x1 - The real questions
Message ID:
200202122224.g1CMOPc00249@vivids1.nsc.com
So basically this
> If, inside Somefunction, you write
> my $var = shift;
> then $var will be the value of $var1
> After that, if you write
> my $var2 = shift;
> then $var2 will be an array reference to @arry.
works in order. The first shift grabs the first variable, the second grabs
the second, and so on?
I think I can.. I think I can..
Thanks again.
On Tuesday 12 February 2002 02:33 pm, Tanton Gibbs wrote:
> Shift removes the first element of the array it is passed...so, take the
> example:
>
> my @arr = (1, 2, 3, 4);
> my $val = shift @arr;
> my $val2 = shift @arr;
>
> print "\$val = $val\n";
> print "\$val2 = $val2\n";
> print "\@arr = @arr\n"
>
> This will print out
> $val = 1
> $val2 = 2
> @arr = 3 4
>
> As you can see, shift removed the first element of the array and returned
> it as its value.
>
> Now, if shift is called without an argument, it uses the default argument
> @_. Convieniently, arguments to a function are passed using @_.
> So, if you call a function:
> f( $var1, $var2 );
> then inside f, $_[0] is the value of $var1 and $_[1] is the value of $var2.
>
> In your specific case,
>
> Somefunction($var1, \@arry);
>
> then $_[0] will be the value of $var1, and $_[1] will be a reference to
> @arry.
>
> If, inside Somefunction, you write
> my $var = shift;
> then $var will be the value of $var1
> After that, if you write
> my $var2 = shift;
> then $var2 will be an array reference to @arry.
>
> HTH,
> Tanton
> ----- Original Message -----
> From: "Steven M. Klass" <Steven.Klass@nsc.com>
> To: "Brett W. McCoy" <bmccoy@chapelperilous.net>
> Cc: <beginners@perl.org>
> Sent: Tuesday, February 12, 2002 4:20 PM
> 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
> >
> > steven.klass@nsc.com
> > http://www.nsc.com
> >
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
--
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
steven.klass@nsc.com
http://www.nsc.com
Thread Previous