use strict;
use warnings;
use Data::Dumper;
my %myHash;
my @array = qw( sean connery george lazemby roger moore timothy dalton pierce brosnan );
print("The array is: @array\n");
for my $i (0..4) {
print("\nElement number $i\n");
my ($key, $val) = splice @array, 0, 2;;
print("The key is: $key\nThe value is: $val\n");
populateHash($key, $val, \%myHash);
print"The hash at cycle $i is: ", Dumper \%myHash;
}
print Dumper \%myHash;
sub populateHash {
my ($key, $val, $hashref) = @_;
print "In the function, the key is $key and the value is $val\n";
$hashref->{$key} = $val;
}
####
$ perl populate_hash.pl
The array is: sean connery george lazemby roger moore timothy dalton pierce brosnan
Element number 0
The key is: sean
The value is: connery
In the function, the key is sean and the value is connery
The hash at cycle 0 is: $VAR1 = {
'sean' => 'connery'
};
Element number 1
The key is: george
The value is: lazemby
In the function, the key is george and the value is lazemby
The hash at cycle 1 is: $VAR1 = {
'sean' => 'connery',
'george' => 'lazemby'
};
Element number 2
The key is: roger
The value is: moore
In the function, the key is roger and the value is moore
The hash at cycle 2 is: $VAR1 = {
'sean' => 'connery',
'roger' => 'moore',
'george' => 'lazemby'
};
Element number 3
The key is: timothy
The value is: dalton
In the function, the key is timothy and the value is dalton
The hash at cycle 3 is: $VAR1 = {
'timothy' => 'dalton',
'sean' => 'connery',
'roger' => 'moore',
'george' => 'lazemby'
};
Element number 4
The key is: pierce
The value is: brosnan
In the function, the key is pierce and the value is brosnan
The hash at cycle 4 is: $VAR1 = {
'timothy' => 'dalton',
'sean' => 'connery',
'roger' => 'moore',
'pierce' => 'brosnan',
'george' => 'lazemby'
};
$VAR1 = {
'timothy' => 'dalton',
'sean' => 'connery',
'roger' => 'moore',
'pierce' => 'brosnan',
'george' => 'lazemby'
};
####
use strict;
use warnings;
use Data::Dumper;
my %myHash;
my @array = qw( sean connery george lazemby roger moore timothy dalton pierce brosnan );
for my $i (0..4) {
my ($key, $val) = splice @array, 0, 2;;
$myHash{$key} = $val;
}
print Dumper \%myHash;
####
$ perl populate_hash.pl
$VAR1 = {
'timothy' => 'dalton',
'sean' => 'connery',
'roger' => 'moore',
'pierce' => 'brosnan',
'george' => 'lazemby'
};