chrishowe has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'm trying to create a set of dynamically named hashes, with an array of references pointing to them.
i.e.

$variable = hash1;
%{$variable} = ("one" => "number");
@array(\%hash1, \%hash2);

I thought of using a hash to hold all the dynamically created hashes, but then that would surely take this structure:

%hash = ( "1" => \%hashref1,
"2" => \%hashref2,);

But then you are going to have to create a dynamically named anonymous hash? (most probably wrong here).
I have had issues with "use strict" and I am fully aware that the world will blow up if I don't use this, and I have used "no strict".
In your opinions, what is the best way to do this?

(btw I have tried and failed :-))
Thanks in Advance
C
  • Comment on Dynamically named hashes (w or w/o strict)

Replies are listed 'Best First'.
Re: Dynamically named hashes (w or w/o strict)
by kennethk (Abbot) on Dec 11, 2008 at 15:21 UTC

    Is there a compelling reason not to just use anonymous hashes for your problem? Why do your variable names need to be set a run-time? For most problems the headaches introduced by not using strict outweigh the benefits.

    use strict; my @array = (); my $hash_ref = {one => "number"}; $hash_ref->{"a"} = "letter"; push @array, $hash_ref; $hash_ref = {}; %{$hash_ref} = (banana => "fruit", carrot => "vegetable"); push @array, $hash_ref; for my $element (@array) { for (sort keys %{$element}) { print "$_ => $element->{$_}\n"; } }

    For for info on arrays, hashes, etc. check out perldata and, for the fancy stuff, perldsc.

      I think that has done the job *and* explained it.
      Cheers :-)
Re: Dynamically named hashes (w or w/o strict)
by moritz (Cardinal) on Dec 11, 2008 at 15:10 UTC
    It's very simple:
    my $variable = { key => "value" }; # or push @array, { key => "value", key2 => $value2 }; # access to the items: say $array[0]{key}; # prints ``value''

    See perldata, perlreftut and perldsc.

      Doesn't that just dynamically create contents for a hash? I'm after a dynamically create name for a hash.
      Or am I being slow and missed your point there? :-)

        One thing is for sure, until you explain to us why you are "trying to create a set of dynamically named hashes, with an array of references pointing to them" we will probably miss your point. :)

        Is there some reason why an anonymous hash won't do exactly what you need without having to have an additional array? After all, you can always create a dynamic list of the hashes keys and values whenever you want.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
        Sorry, I answered your question But then you are going to have to create a dynamically named anonymous hash? and ignored the "named" part, because anonymous and named are contradictory.

        Whenever you think you want variables with variable names, you can just use a hash. In this case you then have a Hash-of-Hash structure. If you want variable named variables nonetheless, search for weak references.

Re: Dynamically named hashes (w or w/o strict)
by Anonymous Monk on Dec 11, 2008 at 15:12 UTC
Re: Dynamically named hashes (w or w/o strict)
by imrags (Monk) on Dec 12, 2008 at 08:21 UTC
    Here's what I did for a dynamically named array...
    @{$arr_name{$i}}
    i had already named $arr_name as "ARR" (not required) but still...
    $i was a counter which created numerous arrays based on requirement.

    Raghu