Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 ( [id://1111331]=note: print w/replies, xml ) Need Help??


in reply to Re: How to populate a HASH using a FOR loop to a FUNCTION
in thread How to populate a HASH using a FOR loop to a FUNCTION

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
  • Comment on Re^2: How to populate a HASH using a FOR loop to a FUNCTION

Replies are listed 'Best First'.
Re^3: How to populate a HASH using a FOR loop to a FUNCTION
by LanX (Saint) on Dec 25, 2014 at 03:47 UTC
    > 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 ☆☆☆☆ :)

Re^3: How to populate a HASH using a FOR loop to a FUNCTION
by Laurent_R (Canon) on Dec 25, 2014 at 12:31 UTC
    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.
Re^3: How to populate a HASH using a FOR loop to a FUNCTION
by Gigiux (Initiate) on Dec 27, 2014 at 17:34 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1111331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found