打印出多级哈希perl 5

时间:2014-08-25 12:29:38

标签: perl hashmap dereference perl-data-structures multi-level

我必须理解这个剧本,不做出重大改变,侮辱制作它的人。我无法更改哈希值,即使将数据加载到数组然后拆分也很容易。写这篇文章的人(我的老板)喜欢他的多级哈希。我必须打印出%extend_hash多级哈希 - 我不明白如何进入最后一级。我需要将其打印到CSV文件中,销售人员可以阅读该文件。 它看起来像6级。 我必须对散列,散列,散列等的键进行排序。

#!/scripts/perl/bin/perl
use strict;
use warnings;
use DBI;

my $dbUser = 'foo_01';
my $dbPass = 'foo_01';
my $dbSid  = 'foo.WORLD';
my $dbh =  DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") or die( "Couldn't connect: $!" );

#sub read_extend

my %extend_hash = ();
my $query = "select level_id,e_risk_symbol,e_exch_dest,penny,specialist from etds_extend";
if(!$dbh) {
    print "Error connecting to DataBase; $DBI::errstr\n";
        }
my $cur_msg = $dbh->prepare($query) or die "\n\nCould not prepare statement: ".$dbh->errstr;
$cur_msg->execute();
while (my @row=$cur_msg->fetchrow_array) {
    $extend_hash{$row[0]}{$row[1]}{$row[2]}{'penny'}=$row[3];
    $extend_hash{$row[0]}{$row[1]}{$row[2]}{'specialist'}=$row[4];
}

for my $what_row0 (sort keys %extend_hash) {
    for my $what_row1 (sort keys %{$extend_hash {$what_row0} }) {
        for my $what_row2 (sort keys ..... I am lost.

我不知道如何将%extend_hash打印到最低级别 我正在尝试将其逗号分隔,能够被输入电子邮件并由销售人员阅读。

6,ACI,ARCX,specialist,1
6,ACI,ARCX,penny,0
6,MCHP,ARCX,specialist,1,
6,MCHP,ARCX,penny,0
6,BC,AMXO,specialist,1
6,BC,AMXO,penny,0
6,WM,XISX,specialist,1
6,WM,XISX,penny,0
6,PK,AMXO,specialist,1
6,PK,AMXO,penny,0
6,SPLS,XISX,specialist,1
6,SPLS,XISX,penny,0

如果我使用Data :: Dumper我得到的这很好,但销售/营销人员会感到困惑。他们将无法在组'6'中看到ARCX penny0。我不认为他们能够通过data :: dump

精神上跳华尔兹
$VAR1 = {
          '6' => {
                    'IACI' => {
                               'ARCX' => {
                                            'specialist' => '1',
                                            'penny' => '0'
                                          }
                             },
                   'MCHP' => {
                                'ARCX' => {
                                           'specialist' => '1',
                                            'penny' => '0'
                                         }
                             },
                    'BC' => {
                             'AMXO' => {
                                         'specialist' => '1',
                                         'penny' => '0'
                                       }
                            },
                    'WM' => {
                             'XISX' => {
                                         'specialist' => '1',
                                         'penny' => '0'
                                       }
                            },
                   'PKD' => {
                              'AMXO' => {
                                           'specialist' => '1',
                                           'penny' => '0'
                                         }
                            },
                    'SPLS' => {
                               'XISX' => {
                                           'specialist' => '1',
                                           'penny' => '0'
                                 }
                    }
          } 
};

更新 - 惊人的工作I_alarmed_alien - 这应该是一个很好的stackoverflow参考(我的评论被禁用)

for my $level_1 (sort keys %extend_hash) {
     for my $level_2 (sort keys %{$extend_hash{$level_1} }) {
        for my $level_3 (sort keys %{$extend_hash{$level_1}{$level_2}}) {
            for my $type (sort keys %{$extend_hash{$level_1}{$level_2}{$level_3}} ) {
                print "$level_1, $level_2, $level_3 $type" . " $extend_hash{$level_1}{$level_2}{$level_3}{$type}" ."\n" ;
                }
         }
    }
}

得到它 - I_alarmed_alien令人惊讶的工作输出散列哈希散列哈希的哈希

6, XLNX, AMXO , specialist 1
6, XLP, AMXO , penny 0
6, XLP, AMXO , specialist 1
6, XLP, XISX , penny 0
6, XLP, XISX , specialist 1
6, XLV, AMXO , penny 0
6, XLV, AMXO , specialist 1
6, XLY, AMXO , penny 0
6, XLY, AMXO , specialist 1
6, YUM, AMXO , penny 0
6, YUM, AMXO , specialist 1
6, ZINC, XISX , penny 0
6, ZINC, XISX , specialist 1
6, ZMH, AMXO , penny 0
6, ZMH, AMXO , specialist 1

4 个答案:

答案 0 :(得分:2)

遍历整个结构并创建逗号分隔表(快速和脏,但工作解决方案):

my $VAR1 = <insert the hash here>

sub printval
{
    my ($val, $path) = @_;
    if (ref($val) eq "HASH")
    {
        printval ($val->{$_}, ($path?"$path,":"")."$_") foreach (keys %{$val});
    }
    else
    {
        print "$path,$val\n";
    }
}

printval($VAR1);

答案 1 :(得分:1)

如果您只想打印它,请使用Data :: Dumper http://perldoc.perl.org/Data/Dumper.html

要检查某个变量是否为哈希引用,请使用:

if (ref($r) eq "HASH") {
  print "r is a reference to a hash.\n";
}

答案 2 :(得分:1)

use Data::Dumper;

print Dumper %extend_hash;

答案 3 :(得分:1)

您的代码几乎就在那里。以下是如何到达哈希的底部:

foreach my $l1 (keys %extend_hash) {
    # '6'
    foreach my $l2 (keys %{$extend_hash{$l1}}) {
        # IACI, MCHP, BC, etc.
        foreach my $l3 (keys %{$extend_hash{$l1}{$l2}}) {
            # ARCX, AMXO, XISX, etc.
            foreach my $k (keys %{$extend_hash{$l1}{$l2}{$l3}}) {
                print "$l1, $l2, $l3, $k, " . $extend_hash{$l1}{$l2}{$l3}{$k} . "\n";
            }
        }
    }
}

请注意,不会以任何特定顺序访问哈希键,因此您可能希望对它们进行排序 - 例如foreach my $l1 (sort keys %extend_hash)

散列哈希哈希的哈希很有趣! ;)

ETA:这是一个更通用的函数,用于递归到任意深度哈希的哈希值...

sub print_hash {
    # href = reference to the hash we're examining (i.e. \%extend_hash)
    # so_far = arrayref containing the hash keys we are accessing
    my $href = shift;
    my $so_far = shift;
    foreach my $k (keys %$href) {
        # put $k on to the array of keys
        push @$so_far, $k;
        # if $href->{$k} is a reference to another hash, call print_hash on that hash
        if (ref($href->{$k}) eq 'HASH') {
            print_hash($href->{$k}, $so_far);
        } else {
        # $href->{$k} is a scalar, so print out @$so_far (our list of hash keys)
        # and the value in $href->{$k}
            print join(", ", @$so_far, $href->{$k}) . "\n";
        }
        # we've finished looking at $href->{$k}, so remove $k from the array of keys
        pop @$so_far;
    }
}

print_hash($hash, []);