webchalkboard has asked for the wisdom of the Perl Monks concerning the following question:

I have an API which i'm trying to connect to. I can send requests in the following format.

my $set1 = { 'setting1' => $setting1, 'type' => 'Broad', 'text' => $text, 'url' => $url, }; my $set2 = { 'setting1' => $setting1, 'type' => 'Broad', 'text' => $text, 'url' => $url, }; my @setargs = [ $set1, $set2 ];

My question is how can I push those set elements onto the setargs array? Rather than explicity defining them, because i'm not sure how many I need to create.

Would code like this work?

foreach my $set (whatever loop) { push @setargs, $set; }


Learning without thought is labor lost; thought without learning is perilous. - Confucius | For the love of art...

Replies are listed 'Best First'.
Re: Pushing Arrays
by xdg (Monsignor) on Aug 15, 2005 at 13:00 UTC

    Be careful what you mean here and whether you want an array :

    my @setargs = ( $set1, $set2 );

    or a reference to an array:

    my $setargs_ref = [ $set1, $set2 ];

    You can push onto either, as long as you refer to it properly

    foreach my $set (whatever loop) { push @setargs, $set; push @{ $setargs_ref }, $set; # explicit dereference }


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Of course, for situations like that, you don't actually need a loop. You can just
      push @setargs, whatever loop
      where whatever loop isn't a loop, but is the list (or array) being iterated over in the original code.

      Caution: Contents may have been coded under pressure.

      Thanks I was meaning a reference to an array, so that code helps a lot thanks,


      Learning without thought is labor lost; thought without learning is perilous. - Confucius | For the love of art...
Re: Pushing Arrays
by ysth (Canon) on Aug 15, 2005 at 13:57 UTC
    Are you sure you meant my @setargs = [ $set1, $set2 ];? If so, and you want to push additional sets onto the array that has $set1 and $set2, do push @{$setargs[0]}, whatever else;. Note that push takes a LIST of things to push; no need to loop and push them one by one.

    If you really meant my @setargs = ( $set1, $set2 );, where the array containing the sets is @setargs itself, not in it's first element, just do push @setargs, whatever else;.

Re: Pushing Arrays
by Roger (Parson) on Aug 15, 2005 at 13:28 UTC
    Umm, what are you trying to do exactly? How are you going to define your sets? If you want to hardcode your settings, you probably should be looking at a better data structure to hold the settings.

    You can use a hash or a list to hold your sets, and use hash slice or array slice to build your arguments the Perl Monk way...

    #hash slice example my %sets = ( 'set1' => { ... }, 'set2' => { ... }, 'set3' => { ... }, ... ); # When you want to build your list of arguments in a # specific order, make use of hash slices my @setargs = @sets{ qw/ set3 set1 set2 / }; # or alternatively, when you want to include everything # and don't care about the ordering my @setargs = @sets{ sort keys %sets };