SPUG: removing array from "array of arrays"
Joel Grow
largest at largest.org
Tue Jun 12 12:35:29 CDT 2001
On Tue, 12 Jun 2001, weaver wrote:
> I have: @args = ("arg1", "arg2", "arg3");
>
> for($i = 1; $i < $something; $i++)
> {
> push @args_list, @args; # or push @args_list, [ @args ];
> }
>
> and in a subroutine would like to remove the @args that I pushed onto
> @args_list during one loop iteration, WITHOUT KNOWING THE # OF
> ELEMENTS ASSIGNED TO @args. In other words, I DON'T want to use:
>
> @var = @_[0, 1, 2];
>
> or anything of this nature.
>
> How can I do this, can it be done?
There is a big difference between
push @args_list, @args;
and
push @args_list, [ @args ];
The former first expands @args into a list and then pushes each element
into @args_list--you do *not* get an array of arrays, just one giant
array.
The latter pushes one element into @args_list for each iteration of the
loop, a reference to an array containing the elements of @args. This is
an array of arrays, and is a good way to solve your problem.
your code might look something like this:
for($i = 1; $i < $something; $i++)
{
push @args_list, [ @args ];
}
# then, later on when you want to process @args_list...
for my $ra_args ( @args_list ) {
process($ra_args);
}
sub process {
my $ra_args = shift;
for my $arg ( @$ra_args ) {
print "I found an arg: $arg\n";
}
}
__END__
$Joel
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list