Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Getting an array slice of value assignments in a hash

by PerlingTheUK (Hermit)
on Jun 03, 2005 at 10:08 UTC ( [id://463198]=perlquestion: print w/replies, xml ) Need Help??

PerlingTheUK has asked for the wisdom of the Perl Monks concerning the following question:

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 };

Cheers,
PerlingTheUK

Replies are listed 'Best First'.
Re: Getting an array slice of value assignments in a hash
by merlyn (Sage) on Jun 03, 2005 at 10:22 UTC
      shouldn't it be :
      map $vals{$_},
      instead ?

Re: Getting an array slice of value assignments in a hash
by tlm (Prior) on Jun 03, 2005 at 12:48 UTC

    The fact that, for 0 <= $i < @arr, $arr[ $i ] == $i makes @arr irrelevant (so the solutions you received before the correction are still valid). But for the general case where this equality does not hold

    print join ',', @arr[ @vals{ sort { $a <=> $b } keys %vals } ];
    ...though I think code should be easier to read than that. I would break that up a bit; e.g.:
    my @sorted_keys = sort { $a <=> $b } keys %vals; print join ',', @arr[ @vals{ @sorted_keys } ];

    the lowliest monk

Re: Getting an array slice of value assignments in a hash
by Tomtom (Scribe) on Jun 03, 2005 at 10:21 UTC
    my %vals=( 1 => '0', 2 => '3', 3 => '5'); # Do you need this ? # my @arr = ( 0,1,2,3,4,5,6 ); print join ',', map { $vals{$_} } sort keys %vals;
Re: Getting an array slice of value assignments in a hash
by ysth (Canon) on Jun 03, 2005 at 10:18 UTC
    Did you mean to be using @arr somewhere?
Re: Getting an array slice of value assignments in a hash
by Roy Johnson (Monsignor) on Jun 03, 2005 at 16:13 UTC
    If you are wondering how the transformations involving map were arrived at, you might want to take a look at Re: Turning foreach into map?, written by tlm, whose response in this thread (somewhat ironically) was slice-based, rather than map-based.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://463198]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found