Code
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 => $DISABL
+E, 48.0 =>$DISABLE, 54.0=>$DISABLE);
foreach my $rate (split(/ /, $DataRates))
{
if(my ($rate1) = $rate =~/basic-(.+)/)
{
print "Data Rate - $rate \n";
%DataRatesHash->{$rate1} = $REQUIRE;
print %DataRatesHash->{$rate1};
print "\n";
}
else
{
print "Data Rate - $rate \n";
%DataRatesHash->{$rate} = $ENABLE;
print %DataRatesHash->{$rate};
print "\n";
}
}
print %DataRatesHash->{2.0} ;
print %DataRatesHash->{1.0} ;
print %DataRatesHash->{5.5} ;
print %DataRatesHash->{48.0} ;
My code prints the following results:
Results
Data Rate - basic-2.0
1
Data Rate - 1.0
0
Data Rate - basic-5.5
1
2212
I do not know why the changes that I have made to the hash values of keys 2.0 1.0 didn't take effect outside the loop
Expected Results
Data Rate - basic-2.0
1
Data Rate - 1.0
0
Data Rate - basic-5.5
1
1012 # Since I modified the hash inside the loop
I would appreciate if someone can point me the error.
Thanks!
2005-10-05 Retitled by planetscape, as per Monastery guidelines
Original title: 'What is wrong with my code?'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.