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.270JIDundef
2. Another test, working and resulting in JHEP. (IF 9.334)9.334JHEPJHEP
3. Last test, which does not work. (IF 1.480)1.480JRNundef

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


In reply to decimal numbers as hash keys by Alex31

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.