in reply to How to populate a HASH using a FOR loop to a FUNCTION
This is a first rewrite of your code, keeping your code architecture but correcting a couple of mistakes and simplifying the code. We are down to 22 code lines:
And this is the output: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 p +ierce 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' };
which will print this: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;
The main difference here is that, considering that populating the hash really takes just 1 line of code, calling a function for just doing this is overkill.$ perl populate_hash.pl $VAR1 = { 'timothy' => 'dalton', 'sean' => 'connery', 'roger' => 'moore', 'pierce' => 'brosnan', 'george' => 'lazemby' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to populate a HASH using a FOR loop to a FUNCTION
by AnomalousMonk (Archbishop) on Dec 25, 2014 at 00:57 UTC | |
by Laurent_R (Canon) on Dec 25, 2014 at 11:40 UTC | |
|
Re^2: How to populate a HASH using a FOR loop to a FUNCTION
by BillKSmith (Monsignor) on Dec 25, 2014 at 03:35 UTC | |
by LanX (Saint) on Dec 25, 2014 at 03:47 UTC | |
by Laurent_R (Canon) on Dec 25, 2014 at 12:31 UTC | |
by Gigiux (Initiate) on Dec 27, 2014 at 17:34 UTC | |
by Laurent_R (Canon) on Dec 27, 2014 at 17:59 UTC | |
|
Re^2: How to populate a HASH using a FOR loop to a FUNCTION
by Gigiux (Initiate) on Dec 27, 2014 at 17:27 UTC |