in reply to Convert arrayref to AoH
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:#!/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 );
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.$ perl defaults.pl $ perl defaults.pl $VAR1 = \[ [ 'kept', '1' ], [ 'repaired', '3' ] ]; $VAR1 = [ { 'value' => '1', 'label' => 'kept' }, { 'value' => '0', 'label' => 'notkept' }, { 'value' => '3', 'label' => 'repaired' } ];
|
|---|