SPUG: removing array from "array of arrays"

Chris Wilkes cwilkes at ladro.com
Tue Jun 12 11:54:49 CDT 2001


What you have listed for the push statement are two dramatically different
items.  The first way you get one long array, like:

@args_list = qw(arg1 arg2 arg3 arg1 arg2 arg3 arg1 arg2 arg3);

with the second way (which I think you want) you get a 2D array that looks
like:

@args_list = [	[arg1 arg2 arg3]
		[arg1 arg2 arg3]
		[arg1 arg2 arg3] ]

The easiest way to find out that you did this is to use the Data::Dumper
module, like this:

  #!/usr/bin/perl
  use Data::Dumper;
  use strict;
  my (@args, @args_list, $something);
  $something = 5;
  @args = ("arg1", "arg2", "arg3");
  for my $i (1..$something) {
    push @args_list, [ $i, @args ];       # or push @args_list, [ @args ];
  }
  print Dumper(@args_list);

(I threw the index variable into the array so that you can see what item
you're removing)
To remove an element out of the @args_list array you have to use the
splice command.  A "perldoc -f splice" command shows you how to use it.

So put in a "splice (@args_list, 3, 1)" in before the Dumper line and
you'll see that you've removed the 4th element (as arrays start at 0) from
your array.

Of course I'm guessing as to if this is actually what you want to do.  I
can't imagine that you're pushing items into a 1D array and then wanting
to remove a group of them w/o knowing which ones to remove.

If you want you can mail me your problem.  It seems like I've joined some
other members of the SPUG list in looking for work ;)

Chris


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?
> 
> Thanks,
> 
> ryan
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      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/
> 
> 
> 
> 



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