Hi monks!

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

$v=~/-([\d]+)$/ ? $1 : undef => [ $k, $v ];
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 ?

-Harold


In reply to HoA create from array using map, more efficient? by hsinclai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.