in reply to hash, deref & looping problem

Just put quotes around your hash keys, to avoid convertion.

use Data::Dumper; use strict; use warnings; my $ENABLE = 0; my $REQUIRE = 1; my $DISABLE = 2; my $DataRates = 'basic-2.0 1.0 basic-5.5'; my %DataRatesHash = ("1.0" => $DISABLE, "2.0" => $DISABLE, "5.5" => $ +DISABLE, "48.0" =>$DISABLE, "54.0"=>$DISABLE); print Dumper(\%DataRatesHash); foreach my $rate (split(/ /, $DataRates)) { if(my ($rate1) = $rate =~/basic-(.+)/) { print "Data Rate 1 - $rate \n"; print "rate1 = $rate1\n"; $DataRatesHash{$rate1} = $REQUIRE; print $DataRatesHash{$rate1}; print "\n"; } else { print "Data Rate 2 - $rate \n"; $DataRatesHash{$rate} = $ENABLE; print $DataRatesHash{$rate}; print "\n"; } } print Dumper(\%DataRatesHash); print $DataRatesHash{"2.0"} ; print $DataRatesHash{"1.0"} ; print $DataRatesHash{"5.5"} ; print $DataRatesHash{"48.0"} ;

Replies are listed 'Best First'.
Re^2: hash, deref & looping problem
by Zed_Lopez (Chaplain) on Oct 04, 2005 at 23:22 UTC

    Just to expand slightly on pg's reply, perldoc perlop says

    The "=>" operator is a synonym for the comma, but forces any word (consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). If the argument on the left is not a word, it is first interpreted as an expression, and then the string value of that is used.

    And you would have gotten away with it, too, if it weren't for that meddling interpretation of non-words as expressions.

Re^2: hash, deref & looping problem
by ramya2005 (Scribe) on Oct 04, 2005 at 23:38 UTC
    Thanks to all who replied! I corrected the code as per your suggestion and now it works!