Unable to create Hash in Perl? -


i learning perl script here. having problem creating hash. code here:

print "hello world!\n"; @days = ("1", "2"); print "there $#days days\n"; print "1 $days[0]\n"; %months = ("a" => 1, "b" => 2, "c" => 3); print "there $#months keys\n"; print "a $months[0]\n"; $i (keys %months) { print "$i has value $months[$i].\n"} 

now working fine array. hash printing "there -1 keys". not printing variable values in last print calls.

you using array syntax on hash, not think @ all. instead of operating on hash, operating on array called @months. example:

print "there $#months keys\n"; 

this array @months, see empty, , happily print -1.

when

for $i (keys %months) {      print "$i has value $months[$i].\n" } 

perl try convert keys a, b , c numbers, 0. issue warning:

argument "a" isn't numeric in array element ... 

then print empty array element $month[0]. issue undefined value warning. not these warnings, because did not use

use strict; use warnings; 

in script. strict have told @months has not been declared, , stopped bug right away.

the syntax should have used is:

print "there " . (keys %months) . " keys\n"; ... print "$i has value $months{$i}\n"; 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -