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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |