Danial Haris
Danial Haris

Reputation: 109

How to increment the value of hash for every subshash Perl

I want to increment the value of hash starting from 0 for each $COMMUNITY, I define an array @indicator from 0 until the same index of the array @NAME and push it to become the value, but it is not I want exactly, and I don't know how to this. I know very well that the value is not in the order because the place in array in not organized first, but how to do this exactly, and then, how to print every first value of subhash $FAMILY. for example,

community 0 = name 0 4 7
community 1 = name 0 3 8
community 2 = name 0 3 6

 

#!/usr/bin/perl 

use warnings;
use strict;
use Tie::Autotie 'Tie::IxHash';


my @NAME= qw(AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AD CD CE CF BD BE);
my @FAMILY= qw(A A A B B B C C C A A A B B B C C C A A A B B B C C C A C C C B B);
my @COMMUNITY= qw(0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 0 2 2 2 1 1);

tie my %COMMUNITY_FAMILY_NAME,'Tie::IxHash' ;
my @indicator;

foreach (my $x=0;$x<=scalar @NAME;$x++)
{
push @indicator,$x;
}


push @{ $COMMUNITY_FAMILY_NAME{ $COMMUNITY[$_] }{ $FAMILY[$_]}{$NAME[$_]} }, $indicator[$_]  for 0 .. $#NAME;
print Dumper(\%COMMUNITY_FAMILY_NAME);

Output:

$VAR1 = {
          '0' => {
                   'A' => {
                            'AA' => [
                                      0
                                    ],
                            'AB' => [
                                      1
                                    ],
                            'AC' => [
                                      2
                                    ],
                            'AD' => [
                                      27
                                    ]
                          },
                   'B' => {
                            'BA' => [
                                      3
                                    ],
                            'BB' => [
                                      4
                                    ],
                            'BC' => [
                                      5
                                    ]
                          },
                   'C' => {
                            'CA' => [
                                      6
                                    ],
                            'CB' => [
                                      7
                                    ],
                            'CC' => [
                                      8
                                    ]
                          }
                 },
           '1' => {
                   'A' => {
                            'AA' => [
                                      9
                                    ],
                            'AB' => [
                                      10
                                    ],
                            'AC' => [
                                      11
                                    ]
                          },
                   'B' => {
                            'BA' => [
                                      12
                                    ],
                            'BB' => [
                                      13
                                    ],
                            'BC' => [
                                      14
                                    ],
                            'BD' => [
                                      31
                                    ],
                            'BE' => [
                                      32
                                    ]
                          },
                   'C' => {
                            'CA' => [
                                      15
                                    ],
                            'CB' => [
                                      16
                                    ],
                            'CC' => [
                                      17
                                    ]
                          }
                 },
           '2' => {
                   'A' => {
                            'AA' => [
                                      18
                                    ],
                            'AB' => [
                                      19
                                    ],
                            'AC' => [
                                      20
                                    ]
                          },
                   'B' => {
                            'BA' => [
                                      21
                                    ],
                            'BB' => [
                                      22
                                    ],
                            'BC' => [
                                      23
                                    ]
                          },
                   'C' => {
                            'CA' => [
                                      24
                                    ],
                            'CB' => [
                                      25
                                    ],
                            'CC' => [
                                      26
                                    ],
                            'CD' => [
                                      28
                                    ],
                            'CE' => [
                                      29
                                    ],
                            'CF' => [
                                      30
                                    ]
                          }
                 }
        };

Expected output:

 $VAR1 = {
              '0' => {
                       'A' => {
                                'AA' => [
                                          0
                                        ],
                                'AB' => [
                                          1
                                        ],
                                'AC' => [
                                          2
                                        ],
                                'AD' => [
                                          3
                                        ]
                              },
                       'B' => {
                                'BA' => [
                                          4
                                        ],
                                'BB' => [
                                          5
                                        ],
                                'BC' => [
                                          6
                                        ]
                              },
                       'C' => {
                                'CA' => [
                                          7
                                        ],
                                'CB' => [
                                          8
                                        ],
                                'CC' => [
                                          9
                                        ]
                              }
                     },
               '1' => {
                       'A' => {
                                'AA' => [
                                          0
                                        ],
                                'AB' => [
                                          1
                                        ],
                                'AC' => [
                                          2
                                        ]
                              },
                       'B' => {
                                'BA' => [
                                          3
                                        ],
                                'BB' => [
                                          4
                                        ],
                                'BC' => [
                                          5
                                        ],
                                'BD' => [
                                          6
                                        ],
                                'BE' => [
                                          7
                                        ]
                              },
                       'C' => {
                                'CA' => [
                                          8
                                        ],
                                'CB' => [
                                          9
                                        ],
                                'CC' => [
                                          10
                                        ]
                              }
                     },
               '2' => {
                       'A' => {
                                'AA' => [
                                          0
                                        ],
                                'AB' => [
                                          1
                                        ],
                                'AC' => [
                                          2
                                        ]
                              },
                       'B' => {
                                'BA' => [
                                          3
                                        ],
                                'BB' => [
                                          4
                                        ],
                                'BC' => [
                                          5
                                        ]
                              },
                       'C' => {
                                'CA' => [
                                          6
                                        ],
                                'CB' => [
                                          7
                                        ],
                                'CC' => [
                                          8
                                        ],
                                'CD' => [
                                          9
                                        ],
                                'CE' => [
                                          10
                                        ],
                                'CF' => [
                                          11
                                        ]
                              }
                     }
            };

Upvotes: 0

Views: 46

Answers (1)

choroba
choroba

Reputation: 241968

You need to iterate the elements by communities, resetting the counter for each. So, I first created the structure with no counters, and then iterated over it in the correct order while setting the values.

#!/usr/bin/perl
use strict;
use warnings;

use Tie::Autotie 'Tie::IxHash';

my @NAMES       = qw(AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AD CD CE CF BD BE);
my @FAMILIES    = qw(A  A  A  B  B  B  C  C  C  A  A  A  B  B  B  C  C  C  A  A  A  B  B  B  C  C  C  A  C  C  C  B  B);
my @COMMUNITIES = qw(0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  0  2  2  2  1  1);

tie my %community_family_name, 'Tie::IxHash' ;

undef $community_family_name{ $COMMUNITIES[$_] }{ $FAMILIES[$_]}{$NAMES[$_]}
    for 0 .. $#NAMES;

for my $community (keys %community_family_name) {
    my $i = 0;
    for my $family (keys %{ $community_family_name{$community} }) {
        for my $name (keys %{ $community_family_name{$community}{$family} }) {
            $community_family_name{$community}{$family}{$name} = [$i++];
        }
    }
}
use Data::Dumper; print Dumper(\%community_family_name);

Note: Are you sure the values need to be in an array ref? There's never more than one value.

By convention, lowercase names are used for mutable variables in Perl.

Upvotes: 2

Related Questions