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.

In reply to Re: How to populate a HASH using a FOR loop to a FUNCTION by Laurent_R
in thread How to populate a HASH using a FOR loop to a FUNCTION by Gigiux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.