neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
Greetings,
I have a default array of hashes. I replace the defaults, if they exist, with an arrayref. Is there a more efficient way to do this?
#!/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', }, ); # Get this from another sub my $a = [ [ 'kept', '1', ], [ 'repaired', '3' ] ]; # Overwrite defaults with arrayref for my $i ( @{ $a } ) { for my $t ( @t ) { if ( $i->[0] eq $t->{label} ) { $t->{value} = $i->[1]; } } } warn Dumper( \$a ); warn Dumper( \@t );
Produces:
$VAR1 = \[ [ 'kept', '1' ], [ 'repaired', '3' ] ]; $VAR1 = [ { 'value' => '1', 'label' => 'kept' }, { 'value' => '0', 'label' => 'notkept' }, { 'label' => 'repaired', 'value' => '3' } ];
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Convert arrayref to AoH
by Athanasius (Archbishop) on Sep 20, 2014 at 14:21 UTC | |
by AnomalousMonk (Archbishop) on Sep 20, 2014 at 20:57 UTC | |
|
Re: Convert arrayref to AoH
by Laurent_R (Canon) on Sep 20, 2014 at 14:20 UTC | |
|
Re: Convert arrayref to AoH
by Laurent_R (Canon) on Sep 21, 2014 at 09:35 UTC |