in reply to Re: HoA create from array using map, more efficient?
in thread HoA create from array using map, more efficient?
... 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" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: HoA create from array using map, more efficient?
by BrowserUk (Patriarch) on Jun 18, 2011 at 19:41 UTC | |
by hsinclai (Deacon) on Jun 18, 2011 at 20:13 UTC | |
by BrowserUk (Patriarch) on Jun 18, 2011 at 21:13 UTC | |
by graff (Chancellor) on Jun 20, 2011 at 00:05 UTC | |
by graff (Chancellor) on Jun 19, 2011 at 23:12 UTC | |
by BrowserUk (Patriarch) on Jun 19, 2011 at 23:49 UTC |