in reply to How to populate a HASH using a FOR loop to a FUNCTION

You are working too much. ;-) Far too much in fact.

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:

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; }
And this is the output:
$ 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' };
Now, this is my second rewrite of your code, now just 12 lines:
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;
which will print this:
$ perl populate_hash.pl $VAR1 = { 'timothy' => 'dalton', 'sean' => 'connery', 'roger' => 'moore', 'pierce' => 'brosnan', 'george' => 'lazemby' };
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.

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
    ... populating the hash really takes just 1 line of code ...

    E.g., for the given example data of the OP:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @array = qw( sean connery george lazenby roger moore timothy dalto +n pierce brosnan ); my %myHash = @array; dd \%myHash; " { george => "lazenby", pierce => "brosnan", roger => "moore", sean => "connery", timothy => "dalton", }

      Sure, you're right. I wanted to start keeping the OP's general architecture while fixing its defects, and then to simplify it in two steps. I originally thought of completing my post with a direct assignment as you did, but I forgot to do it when completing the post. Thanks for doing it. This is of course the easiest solution.
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
    I prefer the OP's approach. Functions should return their results through the return rather than through a reference. I believe that most of his additional code is code that he added to debug the problem himself before he posted. If so, he should be commended. Your use of Dumper and your attention to scope are details he should learn.
    Bill
      > Functions should return their results through the return rather than through a reference.

      Plz be aware that passing and returning hashes as lists can cause much useless overhead, if the original hash has to be overwritten.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Functions should return their results through the return rather than through a reference.
      Hi Bill,

      Yes, I agree in general and often try to write "pure" functions, but I am at the same time generally quite reluctant at the idea of passing around many times a data structure, because this is quite inefficient, as already pointed out by LanX. (Granted, it does not really matter with such small array and hash, but I am usually manipulating fairly large data structures.)

      Anyway, I would not do it the way I described in my first code sample, which aimed at keeping the general code structure of the OP, which was passing a reference to the hash. BTW, this is not very useful here in view of the fact that the hash is in fact a global variable.

      If I wanted to populate the hash in a separate function (which is not really needed in such a simple case, but I am often doing this type of thing when populating the hash is more complicated and requires, for example, to read from one or several file(s) and process the content before actually storing keys and values into the hash), I would call that function only once and pass to it a reference to the array and then return the hash or, when the data is large, return a reference to the hash. Something along these lines:

      use strict; use warnings; use Data::Dumper; my @array = qw( sean connery george lazemby roger moore timothy dalton + pierce brosnan ); my $hash_ref = populate_hash(\@array); print Dumper $hash_ref; sub populate_hash { my $array_ref = shift; my %hash; for (0..4) { my ($key, $val) = splice @$array_ref, 0, 2; $hash{$key} = $val; } return \%hash; }
      But, again, this is only for the sake of argument: for such a simple case, I would not even use a separate function, and the easiest solution is the one provided by AnomalousMonk.
      Dear Bill,
      you are right: all the text I have used herein was just for debugging. The actual function I would like to create would just populate a given hash when called in different points within the main scripts. And at the moment I have no idea what Dumper is...
      G
        My first rewrite of your code kept all the printing that you had in the OP, but was still down to 22 lines versus 45 for yours.

        Dumper is a function of the Data::Dumper module that enables to pretty print in just one command the content of hashes, arrays or more complicated data structures. Very convenient for debugging nested data structures.

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
    Dear Lauren,
    thank you for your reply, I will need some times to digest it.
    Meanwhile, my best wishes of happy new year,
    G