I downloaded that code and ran it as is, and got the following:

Using a hash as a reference is deprecated at test.pl line 20. Using a hash as a reference is deprecated at test.pl line 21. Using a hash as a reference is deprecated at test.pl line 27. Using a hash as a reference is deprecated at test.pl line 28. Using a hash as a reference is deprecated at test.pl line 33. Using a hash as a reference is deprecated at test.pl line 34. Using a hash as a reference is deprecated at test.pl line 35. Using a hash as a reference is deprecated at test.pl line 36.

The warnings about using a hash as a reference are from any line where you've got the following syntax: %hash_ref_name->{keyname}. That's because you're using the wrong sigil. It should be $hash_ref_name->{keyname} (note, the $ instead of the %). But you're also using a named hash, not a hashref, so you don't need the '->' operator. So the correct syntax is actually: $hashname{keyname}. Note the use of '$', and the absence of '->'.

The next issue is the difference between your expected output and the actual output...First quote your keys so they don't get numerified.

  1. The first time through the loop, $rate contains "basic-2.0", so $DataRatesHash{2.0} is set to 1.
  2. The second time through the loop, $rate contains "1.0", so $DataRatesHash{1.0} is set to 0.
  3. The third time through the loop, $rate contains "basic-5.5", so $DataRatesHash{5.5} is set to 1.
  4. $DataRatesHash{48.0} remains unchanged at 2.

So when you print @DataRatesHash{ '2.0', '1.0', '5.5', '48.0' } (please excuse the shorthand), you get the expected 1012.


Dave


In reply to Re: hash, deref & looping problem by davido
in thread hash, deref & looping problem by ramya2005

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.