Dear all,
I am trying to learn Perl and I am stuck with the hash section. I would like to implement a function (subroutine) that I could call when required to populate the hash with new elements.
The way I have done it is to create a FOR loop in which at each cycle a function is called; the function gets the key and value and the link to the hash. The function then returns the hash.
In theory the function gets the variables directly from the keyboard but for this example I have set a variable with some strings.
The function I have implemented actually properly gets the variables and store them in the hash, however at each cycle the hash is reset so the hash is not populated but just hosts the last couple of values that is fed with.
Is there a way to store the values permanently, so that the hash can be populated a bit at the time?
Thank you for any help.
Gigiux
here is the code i have written
use strict; my(@myArray); my(%myHash); my($myLoop, $key, $val, $myTemp, $i); @myArray = qw( sean connery george lazemby roger moore timothy dalton +pierce brosnan ); $myLoop = 5; print("The array is: @myArray\n"); for($i = 0; $i < $myLoop; $i++){ print("\nElement number $i\n"); $key = shift(@myArray); $val = shift(@myArray); print("The key is: $key\nThe value is: $val\n"); %myHash = &makeHash($key, $val, \%myHash); print("The hash at cycle $i is: "); print %myHash; $myTemp = $myHash{$key}; print("\nThe actual value for \"$key\" is: $myTemp\n"); } print("\nThe hash is not functional, in fact the value for \"sean\" is +: "); $key = "sean"; $myTemp = $myHash{$key}; print $myTemp; print("\n(that was an empty result)\n"); sub makeHash { my($key); my($val); my(%subHash); $key = shift @_; $val = shift @_; %subHash = shift @_; print("In the function, the key is $key and the value is $val\n"); print("The hash at the beginning is: "); print %subHash; $subHash{$key} = $val; print("\nThe hash at the end is: "); print %subHash; print("\n"); return(%subHash); }
the result from the first two cycles and at the end of the loop is as follows:
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 the beginning is: HASH(0x1bfd938) The hash at the end is: HASH(0x1bfd938)seanconnery The hash at cycle 0 is: HASH(0x1bfd938)seanconnery The actual value for "sean" is: 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 the beginning is: HASH(0x1bfd938) The hash at the end is: georgelazembyHASH(0x1bfd938) The hash at cycle 1 is: HASH(0x1bfd938)georgelazemby The actual value for "george" is: lazemby ... The hash is not functional, in fact the value for "sean" is: (that was an empty result)
Note: even if I remove the link to the hash, the results are slightly different ("sean" is kind of stuck to the array but it is lost at the end of the implementation) but the hash does not get properly populated.
In reply to How to populate a HASH using a FOR loop to a FUNCTION by Gigiux
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |