This is the type of thing where a subroutine can be a very useful abstraction:

frobnitz( $HoA{Foo} ) if $HoA{Foo}; for my $key ( keys %HoA ) { frobnitz( $HoA{$key} ) if $key ne "Foo"; }

If you can't factor it out nicely as a subroutine, you can consider factoring it out as an anonymous code ref so that you can use your lexical variables in it.

Another possibility:

my %HoA= ( D=>[4], E=>[5], F=>[6], Foo=>[3], G=>[7], H=>[8], ); { local( $HoA{Foo} )= $HoA{Foo}; for my $av ( delete $HoA{Foo}, values %HoA ) { print $av->[0], $/ if $av; } } print $HoA{Foo}[0], $/;

But I'd not use that in real® code (and it likely depends on things that at least should be "undefined").

(update) Or use sub to abstract this out differently:

sub FirstIfKeys { my $hv= pop @_; my %k; @k{ keys %$hv }= (1) x @k; my @k; for my $key ( @_ ) { push @k, $key if delete $k{$key}; } return @k, keys %k; } for my $key ( FirstIfKeys( "Foo", \%HoA ) ) { ... }

- tye        


In reply to Re: Pulling an item to the front of the list (sub) by tye
in thread Pulling an item to the front of the list by Tanktalus

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.