Perl hashes are not associative arraysassociation lists, though I can see how one might think that way if one comes from a Lisp background. Please take a careful look at perldata and perldsc for a better understanding of Perl hashes and how to work with them.
How did you set up @data_list? The code above would only print out a value if you had set up @data_list something like this:
my @data_list = ( { correct => 42 } );
Or maybe you did something like this: my %data_list =(correct=>42)? That does not set up a collection of data pairs even though the fat comma (=>) may make it seem that way. To iterate through the elements of %data_list using a for loop one would need to use keys and do something like this:
foreach my $k (keys %data_list) {
my $v = $data_list->{$k};
print "The value for $k is $v\n";
}
@data_list=(correct => 42) doesn't set up discrete pairs either. It merely sets up a flat array with alternating keys and values. To iterate through key value pairs for @data_list one would need to do something like this:
for (my $i=0; $i < $#data_list; $i+=2) {
my $k = $data_list[$i];
my $v = $data_list[$i+1];
print "The value for $k is $v\n";
}
Note that in Perl, %data_list and @data_list are two entirely different variables. If the preceding discussion does not clarify things, I think you will need to post more code so we can see how @data_list and/or %data_list is set up.
Best, beth
Update - struck out "associative array" and replaced with "association list" - see Re^5: Associative array for explanation. |