Hmm, thinking more about your problem, assuming you really need your array of hashes for some other reasons than what you have shown us, and also assuming that your labels are unique, you could build an auxiliary hash of hashes providing direct access to the values that you want to update, through the magics of references, i.e. store references to the data items that you need to change in an additional hash.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Defaults my @t = ( { 'label' => 'kept', 'value' => '0', }, { 'label' => 'notkept', 'value' => '0', }, { 'label' => 'repaired', 'value' => '0', }, ); # Create another structure (hash) to access the same data in a faster +way my %faster_access; for my $valref (@t) { $faster_access{$valref->{'label'}} = $valref; } # Get this from another sub my $a = [ [ 'kept', '1', ], [ 'repaired', '3' ] ]; # Overwrite defaults for my $i ( @{ $a } ) { $faster_access{$i->[0]}{value} = $i->[1]; } warn Dumper( \$a ); warn Dumper( \@t );
The auxiliary HoH makes it possible to access directly (and much faster) to the data that you want to update. This is the output of the program:
$ perl defaults.pl $ perl defaults.pl $VAR1 = \[ [ 'kept', '1' ], [ 'repaired', '3' ] ]; $VAR1 = [ { 'value' => '1', 'label' => 'kept' }, { 'value' => '0', 'label' => 'notkept' }, { 'value' => '3', 'label' => 'repaired' } ];
As you can see, the @t array has been modified indirectly thanks to the changes made to the %faster_access containing references to the values that you need to update. There is no longer a need to browse through the whole AoH when you want to update it, which is handy if you need to update many times in the course of your program execution.

In reply to Re: Convert arrayref to AoH by Laurent_R
in thread Convert arrayref to AoH by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.