Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: hash, deref & looping problem

by pg (Canon)
on Oct 04, 2005 at 23:12 UTC ( [id://497434]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://497434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found