in reply to Comparing hashes and arrays
map{/(\S+)\s+([\d.]*)/;$2?($a{$1}=$2):($a{$1}=999)}<FILE>; map{push(@result,$a{$_})}@order;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing hashes and arrays
by Sifmole (Chaplain) on Sep 06, 2001 at 16:38 UTC | |
Update I don't believe this returns the right result either. I am still checking out why, will update shortly. Update 2:
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.
The resulting fixed code...
Perhaps we should call golf-on? | [reply] [d/l] [select] |
by bbfu (Curate) on Sep 06, 2001 at 21:37 UTC | |
$2 ? ... will give the wrong result [...] This can be fixed using $2 ne '' Well, it seems to me from the original post that the file will always have both columns; if the match doesn't succeed then the line is not valid and (maybe?) shouldn't be considered. The code becomes (note that map in void context is almost always considerably slower than for):
Remember, this simply ignores lines in the file that don't have both columns. It also ignores a second decimal (and everything after) in the second column. It all depends on how lenient we want to be of bad data. bbfu | [reply] [d/l] [select] |
by Anarion (Hermit) on Sep 06, 2001 at 22:02 UTC | |
If the second value is null the re doesn't match and $2 is the one of the match before. Update: Sorry i dont read that bbfu has fixed it adding ? $anarion=\$anarion; s==q^QBY_^=,$_^=$[x7,print | [reply] [d/l] |
by Sifmole (Chaplain) on Sep 06, 2001 at 22:53 UTC | |
If the second value is null as in something like The the entire pattern will not match since the second value is required according to the regex. Also, a simple test of adding a print statement during the match will educate you that the $1 and $2 are not carried over. Run this to see... Notice the added print line will show you that $1 and $2 are empty, not the previous values. So, to conclude -- actually running it would have shown you that there was no error. | [reply] [d/l] [select] |