Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

That is not a reason to want to do it the way it is in your mind right now.

It is rather funny to see this topic, as it is almost exactly the problem that dragged me into perl development in the first place. I almost did the same, and once everything "worked", problems started to arise.

The solution, as others already stated in a more or less explicit way, is to make ONE (and only ONE) global (or at least in a scope as limited as possible) variable that contains all your hashes that you do not yet know about.

my %all_my_hashes; $all_my_hashes{$variable} = { valorC => "value03", valorD => "value04" +, }; print $all_my_hashes{$variable}, "\n"; # later you read ; records while (<>) { # consider Text::CSV_XS with sep_char => ";" my @alelos = split ";" => $_, -1; while (@loci_codes) { $variable = shift @loci_codes; $all_my_hashes{$variable}; my $allele1 = shift @alelos; my $allele2 = shift @alelos; $all_my_hashes{$variable}{$allele1) += 1; $all_my_hashes{$variable}{$allele2} += 1; } $all_my_hashes{Bet01} = { 230 => 2, 238 => 5, 224 => 1, }; $all_my_hashes{Bet05} = { 101 => 2, 103 => 2, 0 => 4, };

Note that the main difference is that you'd need to initialize as anonymous hashes instead of lists, so

%$foo = ( 1, 2, ... ); => my %all_my_hashes; $all_my_hashes{$foo} = { 1 => 2, ... };

Of course you can also use a ref as top-level, so the reference stands out better (for any value of better):

my $all_my_hashes; $all_my_hashes->{$foo} = { 1 => 2, ... };

One *huge* advantage with using a scalar reference is that you can easily (and safe and fast) pass that reference around to other parts of your program and limit the scope.

Aut-vivivication is your friend!

Enjoy, Have FUN! H.Merijn

In reply to Re^5: variable as hash name by Tux
in thread variable as hash name by blackzero

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2018-06-23 16:46 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.