@array=@_[2..$#_-1];

W

Winston

Why this is not work?
@array=@_[2..$#_-1];

I give to a function 2 variables and 1 array, y need to take the whole
array.
 
P

Paul Lalli

Winston said:
Why this is not work?
@array=@_[2..$#_-1];

Define "not work". The above assigns @array to be all elements from
@_, except for the first, second, and last. Is that what you intended?
I give to a function 2 variables and 1 array, y need to take the whole
array.

I have no idea what this means. By "give to a function", I assume you
mean "pass as arguments"? By "2 variables", do you mean 2 scalars?
Do you understand that arrays are variables too? No idea what "need to
take the whole array" means.

Please post a short-but-complete script that demonstrates whatever
error you're talking about.

Paul Lalli
 
D

DJ Stunks

Winston said:
Why this is not work?
@array=@_[2..$#_-1];

I give to a function 2 variables and 1 array, y need to take the whole
array.

sub whatever {
my ($var1, $var2, @array) = @_;

# whatever
}

?

-jp
 
P

Paul Lalli

Winston said:
Why this is not work?
@array=@_[2..$#_-1];
I give to a function 2 variables and 1 array, y need to take the whole
array.

Taking another look at this, I think *maybe* I understand what you're
asking. My *guess* is that you have the above line in your subroutine,
and you are passing that subroutine two scalars followed by an array.
And you're expecting @array to contain all the elements of the array
you passed to the subroutine. But it's missing the final element.

Is my guess correct?

If so, your actual problem is not realizing that $#_ is the last
*index* of @_, not the size. Therefore, you should not be subtracting
1 from it. Remove "-1" and it will do what you want.

In reality, however, that's an awful piece of code, and you should
trash it immediately. Does your subroutine look something like this?

sub fctn {
my $foo = $_[0];
my $bar = $_[1];
my @baz = @_[2..$#baz];
# ...
}

If so, please replace it with:

sub fctn {
my ($foo, $bar, @baz) = @_;
#...
}

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top