Given the following variable:
$test = {
'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => 'G',
'5' => 'K',
}
How can loop through all assignments without knowing which keys I have?
I would like to fill a select box with the results as label and the keys as hidden values.
Just do a foreach loop on the keys:
#!/usr/bin/perl
use strict;
use warnings;
my $test = {
'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => 'G',
'5' => 'K',
};
foreach my $key(keys %$test) {
print "key=$key : value=$test->{$key}\n";
}
output:
key=4 : value=G
key=1 : value=A
key=3 : value=C
key=2 : value=B
key=5 : value=K
You can use the built-in function each:
while (my ($key, $value) = each %$test) {
print "key: $key, value: $value\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With