Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: variable as hash name

by eyepopslikeamosquito (Canon)
on Dec 08, 2012 at 06:06 UTC ( #1007860=note: print w/ replies, xml ) Need Help??


in reply to Re^2: variable as hash name
in thread variable as hash name

%Bet01 = ( 230 => 2, 238 => 5, 224 => 1,); %Bet05 = ( 101 => 2, 103 => 2, 0 => 4, );
Why not simply have "Bet01", "Bet05", ... as keys in a hash? For example:
my %bets = ( 'Bet01' => { 230 => 2, 238 => 5, 224 => 1 }, 'Bet05' => { 101 => 2, 103 => 2, 0 => 4 }, );


Comment on Re^3: variable as hash name
Select or Download Code
Re^4: variable as hash name
by blackzero (Acolyte) on Dec 08, 2012 at 09:37 UTC

    Because these 'groups' names ( that I want to use as hash names ) will also come from a external file.

      Because these 'groups' names ( that I want to use as hash names ) will also come from a external file.

      Exactly, they can all go into a hash, you can have

      $hashref->{group1}{dogs}{fido}{breed}='doberman'; $hashref->{group1}{dogs}{fido}{age}=12; $hashref->{group11}{humans}{larry}{age}= undef; ...
      or anything -- should really read varvarname

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-10-02 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls