Alex31 has asked for the wisdom of the Perl Monks concerning the following question:
I have decimal numbers as hash keys. These numbers are coming from a text stored in $title and parsed by a REGEX. It works, unless there is a 0 on last decimal position.
The file test.txt contains a title (one per line) (see table below - $title)
%ifac= ( 14.152 => "JCI", 9.334 => "JHEP", 5.745 => "JIMM", 6.270 => "JID", 1.480 => "JRN", 6.105 => "KI" ); open (FH, "<test.txt"); my $zler=0; while(defined(my $title=<FH>)) { if ($title=~m|.{10,}\(IF ([\.\d]+)\)|) { $id=$zler; $factor=$1; $jour=$ifac{$factor}; print "INSERT INTO table (id, key, value) VALUES ($id,'ifac','$jou +r');\n"; } $zler++; } close(FH);
Result of $title, $factor, $jour would be for example:
| $title | $factor | $jour (expected) | $jour (as it is) |
|---|---|---|---|
| 1. This is a test. (IF 6.270) | 6.270 | JID | undef |
| 2. Another test, working and resulting in JHEP. (IF 9.334) | 9.334 | JHEP | JHEP |
| 3. Last test, which does not work. (IF 1.480) | 1.480 | JRN | undef |
If $factor results in 6.270 or 1.480 then $jour is undefined but should result in JID or JRN.
I tried a direct access to the hash with
$jour= $ifac{6.270} $jour= $ifac{6.27} $jour= $ifac{"6.270"}
In these cases $jour results in JID (except for the "6.270" undefined (this is OK, as the hash key is a number and not a string)
What is the difference between the direct access and access of hash key via the variable $ifac{$factor}? It seems that the last 0 is the problem. But in direct access it would work, even if I leave out the last 0.
Regards, Alex
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: decimal numbers as hash keys
by kennethk (Abbot) on Jun 26, 2013 at 15:27 UTC | |
by Alex31 (Initiate) on Jun 26, 2013 at 15:53 UTC | |
by kennethk (Abbot) on Jun 26, 2013 at 18:11 UTC | |
|
Re: decimal numbers as hash keys
by arnaud99 (Beadle) on Jun 26, 2013 at 15:51 UTC | |
|
Re: decimal numbers as hash keys
by locked_user sundialsvc4 (Abbot) on Jun 26, 2013 at 16:28 UTC |