Dear Monks,
I have this piece of code:
my %vals=( 1 => '0', 2 => '3', 3 => '5');
my @arr = ( 0,1,2,3,4,5,6 );
my @res;
for my $key ( sort{ $a <=> $b } keys %vals ){
push @res, $arr[$vals{ $key }];
}
print join (',', @res );
I am not good at writing compact code but I did not see any way to simplify this to a one liner or so. I am looking forward to your suggestions.
I tried to somehow get an array slice that would go into a line like
print join (',', @arr[@res] );
but then thought there must be an even more compact way.
UPDATE: I changed one line in the above code to show what I wanted to use @arr for. Thanks ysth. The old line was
push @res, $vals{ $key };
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.