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.
This code has two errors, and one potential gotcha.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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |