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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -