I want to see if I can make the following more efficient, or if it's worth the bother..
I'm parsing through some listing results from the EC2 api commands, getting lists of snapshots/volumes; I have an array @snapshot_listing, it's a bunch of lines and looks like this..
TAG snapshot snap-162c4c78 Name rotation-snap-msgin-msginvol +-vol-28fb2141-1306254741 TAG snapshot snap-182ce47a Name rotation-snap-msgin-msginvol +-vol-22fb2a40-1306253728 TAG snapshot snap-3e02ee50 Name rotation-snap-util-backupvol +-vol-11cb3d7a-1307246947 TAG snapshot snap-133c4c7e Name rotation-snap-msgin-msginvol +-vol-28fb2140-1306254729 TAG snapshot snap-112ae52a Name rotation-snap-msgin-msginvol +-vol-28fb2140-1307254721 TAG snapshot snap-2e0a6e5f Name rotation-snap-util-backupvol +-vol-14ca3d7a-1308456945
My little name tags have a timestamp on the end, so I want to extract that out when I build up the following hash, so I can decide which objects are old enough to delete.. I have an hoa where the key is my timestamp..
my %snapshot_roster = map { ($k,$v) = (split /\s+/,$_)[2,4]; strip_stamp($v) => [ $k, $v ]; } @snapshot_listing; sub strip_stamp { /-([\d]+)$/ and return $1; }
I could get rid of the subroutine, and do the string construction for the hash key on the fly
and although this works, it somehow seem dangerous and not the safest way to do it... are there cases where $v could get overwritten (it's not right now somehow), and undef seems like the wrong other choice if by chance the string comparison doesn't have my expected timestamp on the end... maybe time() instead of undef .. any suggestions on how to better think this through ?$v=~/-([\d]+)$/ ? $1 : undef => [ $k, $v ];
-Harold
In reply to HoA create from array using map, more efficient? by hsinclai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |