[oclug] Perl Q. (accessing anonymous hash of array)

Joe Burpee jeb at burkby.com
Tue Jun 24 20:14:20 EDT 2003


On Tue, Jun 24, 2003 at 02:04:27PM -0400, Jon Earle wrote:
> I have a function which creates a list of users.  I want to pass that list
> to another function via an anonymous hash, thus:
> 
>     my @users = $query->param('users');
>     return unless @users;
> 
>     unless ($query->param('conf')) {
>         &confirm_delete('delete', {'users' => [ @users ]});
>         exit;
>     }
> 
> and confirm_delete has relevant lines, thus:
> 
>     my ($action, $hashref) = @_;
> 
>     foreach (@$hashref{'users'}) {
>         print "<input type='hidden' name='users' value=\"$_->[0]\">\n";
 
I think you're passing the hash ok, but I'm not so sure the lines in
confirm_delete are "relevant" or correct.

If you really do want to iterate over the data in the array, you
could dereference the array completely in the foreach():

      foreach (@{$$hashref{'users'}}) {
          print "<input type='hidden' name='users' value=\"$_\">\n";

But if you want to print the full name list without iterating, you
probably just need a single statement like:

      print "<input type='hidden' name='users' value=\"",
            join(' ', @{$$hashref{'users'}}),
            "\">\n";
 
> Trouble is, I only get the first entry from the @users array.  Can
> anyone spot what I'm doing wrong?
 
You ended up dereferencing the right number of times, so you did get to
the data, but only in element [0].

I think there are some relevant examples in man perlref.

Joe



More information about the OCLUG mailing list