in reply to hash, deref & looping problem

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