... so long as other things don't impede normal operation (disk corruption, interrupted network connection, ...). Naturally, things can happen "by chance" that do cause perturbation, so some amount of "paranoia" about expected data formats getting bollixed is always justified, I think.
But in this case, your proposed solution (%hash = map {/(.)(.)/} @array;) has the nice property that there will only be hash elements created for records that have the expected (key, value) content. In terms of error checking, it might suffice just to know how many input records didn't match:
If it's important to know what was in the entries that failed, the map block would need some elaboration -- and I think a subroutine is needed (but at least it only gets called when a record fails to match):if ( my $lost = @snapshot_listing - keys %snapshot_roster ) { warn sprintf( "There were %d bad entries in %d input records\n", $lost, scalar @snapshot_listing ); }
my @bad_entries; sub nfg { push @bad_entries, $_; return } my %snapshot_roster = map { m[(snap-\S+).*+-(\d+)$] ? ($1,$2) : nfg() } @snapshot_listing; if ( @bad_entries ) { warn "oh well... win some, lose some.\n" }
In reply to Re^2: HoA create from array using map, more efficient?
by graff
in thread HoA create from array using map, more efficient?
by hsinclai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |