That is probably a pretty good start on a golf answer. I do wonder however whether it was at all useful to a user who asks this level of question. Perhaps you could explain how it works to the inquiring monk, that would make it more valueable.

Update I don't believe this returns the right result either. I am still checking out why, will update shortly.

Update 2:
The solution you presented fails to produce the requested 999 result in the case of the missing entry. It also fails to capture the negative value, -7.9.

I will break down the provided solution so the questioner can perhaps understand what is going on better.

map{ # map is a way of building a loop # map returns a list of the resulting values. # this use of map makes no use of the returned list # which is often considered bad form, however when # golfing it can be useful for shortening your code. /(\S+)\s+([\d.]*)/; # this is a regular expression # (\S+) says to grab 1 or more # non-white-space characters, # the result captured to $1 # \s+ says to grab 1 or more # white-space characters # ([\d.]*) says to grab either # digits (\d) or a period (.) # 0 or more times. # this will be captured to $2 $2 # This is a a ternary operator # A sometimes useful way of # writing an if-else statement. # This says "if $2" ? ( $a{$1} = $2 ) # then set $a{$1} to $2 : ( $a{$1} = 999 ) # else set $a{$1} to 999 }<FILE>; # the lines read from <FILE> will be used # as input to the map, as $_ map{push(@result,$a{$_})}@order; # again a map, taking the order array and pushing the # related values from the $a hash into @results # giving you the ordered numbers.
This code has two errors, and one potential gotcha.

The resulting fixed code...

map{/(\S+)\s+(-?[\d.]+)/;$a{$1}=$2?$2:''}<DATA>; map{push(@result,defined $a{$_} ? $a{$_} : 999)}@order;

Perhaps we should call golf-on?


In reply to Re: Re: Comparing hashes and arrays by Sifmole
in thread Comparing hashes and arrays by Evanovich

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.