In Perl, why is the order of `values %hash` different than the order in the list I used to create %hash? -
i extracting array hash, , have printed array can see order of output different. why this?
my code:
#!/usr/bin/perl %data = ('john paul' => 45, 'lisa' => 30, 'kumar' => 40); @ages = values %data; print "$ages[0]\n"; print "$ages[1]\n"; print "$ages[2]\n";
hash tables not have order. if want values out in specific order, can either iterate through keys in order:
for $person (('john paul', 'kumar', 'lisa')) { $age = $data{ $person }; ...
or, use hash slice desired key order:
@ages = @data{ ('john paul', 'kumar', 'lisa') };
see how can make hash remember order put elements it? in faq list.
you should read faq list @ least few times.
Comments
Post a Comment