在 Perl 中查看哈希的哈希?

发布于 2024-11-29 23:00:54 字数 4849 浏览 2 评论 0原文

当我尝试遵循存储的引用(在提取 Tripwire/get Data 子例程中设置)并将其转换回散列(在比较子例程中)时,即 %hash = %{$DataHash{$key}}; ,然后我尝试打印密钥。我遇到这些问题:

在数组取消引用中使用未初始化的值 $hash{"ElementName"} ... 这是@hashItems = @{$hash{ElementName}};线

在 print at .... 中使用未初始化的值 print "Data: ", $hash{ElementName}, "\n";线

sub extract_tripwire{

    foreach $server (@servers){

        $server = lc $server;

        my $xlfile = "";
        $xlfile .= "";


        # Open the provided Excel sheet
        my $book = $xl->Workbooks->Open($xlfile);

        # Setip active worksheet
        my $sheet = $book->Worksheets(1);

        # Finds the Last row that has data in it
        my $LastRow = $sheet->UsedRange->Find({What=>"*",
            SearchDirection=>xlPrevious,
            SearchOrder=>xlByRows})->{Row};

        my $LastCol = $sheet->UsedRange->Find({What=>"*", 
              SearchDirection=>xlPrevious,
              SearchOrder=>xlByColumns})->{Column};

        print "Last Row: $LastRow, Last Column: $LastCol\n";

        #This will be a reference to a hash
        if($LastRow > 1){
            my %Data = %{&get_Data($LastRow,$LastCol,$sheet)};
        }
        else{
            print "No program Changes\n";
        }

        # Close the workbook when done
        $xl->ActiveWorkbook->Close(0);

        #Maybe Store a reference to the hash?
        $DataHash{$server} = \%Data;
    }

    # Get the names of the columns from the given excel file
    sub get_Data{
        # Initialization of variables 
        my @header;
        my @data;
        my %Data;

        # Print out all the data
        my $array = $_[2]->Range("A1:I1")->{'Value'};

        # Cycle through A1->I1 , A1->B1->C1->D1...
        foreach my $ref_array (@$array){
            foreach my $scalar(@$ref_array){
                    push @header, $scalar;
            }
        }

        #The letters that are associated with the columns of the excel file
        my @letters = ('A','B','C','D','E','F','G','H','I');

        my $counter = 0;

        # Loop through all the columns and store the data within a hash with the specific header as its key
        for($index = 0; $index < scalar @letters; $index++,$counter++){
            # The range of the column
            my $array = $_[2]->Range("$letters[$index]2:$letters[$index]$_[0]")->{'Value'};

            # Cycle through A2->I4 , A2->A3->A4->...->I2->...
            foreach my $ref_array (@$array){
                foreach my $scalar(@$ref_array){
                    if(defined($scalar)){
                        push @data, $scalar;
                    }
                    else{
                        $scalar = " ";
                    }
                }
            }
                $Data{$header[$counter]} = @data;
                @data = ();                             
        }
        # Return the data hash
        return \%Data;
    }

    &Compare(\%DataHash);
}
sub Compare{
    # Get the hash of a has that was created from the tripwire reports
    my %Datahash = %{$_[0]};

    # Get the keys of that file
    @keys = sort keys %DataHash;

    foreach(@keys){
        print "Keys: $_\n";
    }

    #Loop through the keys
    foreach my $key (@keys){
----------> #This is the Main PROBLEM in the code
----------> %hash = %{$DataHash{$key}};

        # Get all the keys of that hash
        @hashKeys = keys %hash;

                    print "Data: ", $hash{ElementName}, "\n";

        #Get the items Programs that has been implemented
        @hashItems = @{$hash{ElementName}};

        #Try and match them up against the Task from Alfresco
        for($i = 0; $i < scalar @hashItems;$i++){
            for($j = 0; $j < scalar @promoCode; $j++){
                #Split up the PromoCode Here!!!! 0- File name, 1- Task #
                #If a match has been found
                if($hashItems[$i] ~~ $promoCode[$j]){
                    # Extract the row that match
                    foreach (@hashKeys){
                        @array = @{$hash{$_}};
                        #And store it in this array
                        push @pass, $array[$i];
                    }
                    # So that the information can be passed to the Reconcile routine to be added to the Details and Summary reports + Task #
                    &Reconcile(@pass,$i);

                    #Need insert and empty row
                    $sheet->Range("A$lastRow:G$LastRow")->Select;
                    $xl->Selection->Interior->{ColorIndex} = 15;
                    $xl->Selection->Interior->{Pattern} = xlSolid;
                }
            }
        }

    }
}'

我​​创建哈希的哈希的方式有问题吗?我读得怎么样?

When I try to deference the stored reference (which is set up in the extract Tripwire/get Data subroutines) and convert it back to a hash(in the Compare subroutine), ie %hash = %{$DataHash{$key}};, and I try to print the keys. I run into these problems:

Use of uninitialized values $hash{"ElementName"} in array dereference at line...
This is at @hashItems = @{$hash{ElementName}}; line

Use of uninitialized value in print at .... print "Data: ", $hash{ElementName}, "\n"; line

sub extract_tripwire{

    foreach $server (@servers){

        $server = lc $server;

        my $xlfile = "";
        $xlfile .= "";


        # Open the provided Excel sheet
        my $book = $xl->Workbooks->Open($xlfile);

        # Setip active worksheet
        my $sheet = $book->Worksheets(1);

        # Finds the Last row that has data in it
        my $LastRow = $sheet->UsedRange->Find({What=>"*",
            SearchDirection=>xlPrevious,
            SearchOrder=>xlByRows})->{Row};

        my $LastCol = $sheet->UsedRange->Find({What=>"*", 
              SearchDirection=>xlPrevious,
              SearchOrder=>xlByColumns})->{Column};

        print "Last Row: $LastRow, Last Column: $LastCol\n";

        #This will be a reference to a hash
        if($LastRow > 1){
            my %Data = %{&get_Data($LastRow,$LastCol,$sheet)};
        }
        else{
            print "No program Changes\n";
        }

        # Close the workbook when done
        $xl->ActiveWorkbook->Close(0);

        #Maybe Store a reference to the hash?
        $DataHash{$server} = \%Data;
    }

    # Get the names of the columns from the given excel file
    sub get_Data{
        # Initialization of variables 
        my @header;
        my @data;
        my %Data;

        # Print out all the data
        my $array = $_[2]->Range("A1:I1")->{'Value'};

        # Cycle through A1->I1 , A1->B1->C1->D1...
        foreach my $ref_array (@$array){
            foreach my $scalar(@$ref_array){
                    push @header, $scalar;
            }
        }

        #The letters that are associated with the columns of the excel file
        my @letters = ('A','B','C','D','E','F','G','H','I');

        my $counter = 0;

        # Loop through all the columns and store the data within a hash with the specific header as its key
        for($index = 0; $index < scalar @letters; $index++,$counter++){
            # The range of the column
            my $array = $_[2]->Range("$letters[$index]2:$letters[$index]$_[0]")->{'Value'};

            # Cycle through A2->I4 , A2->A3->A4->...->I2->...
            foreach my $ref_array (@$array){
                foreach my $scalar(@$ref_array){
                    if(defined($scalar)){
                        push @data, $scalar;
                    }
                    else{
                        $scalar = " ";
                    }
                }
            }
                $Data{$header[$counter]} = @data;
                @data = ();                             
        }
        # Return the data hash
        return \%Data;
    }

    &Compare(\%DataHash);
}
sub Compare{
    # Get the hash of a has that was created from the tripwire reports
    my %Datahash = %{$_[0]};

    # Get the keys of that file
    @keys = sort keys %DataHash;

    foreach(@keys){
        print "Keys: $_\n";
    }

    #Loop through the keys
    foreach my $key (@keys){
----------> #This is the Main PROBLEM in the code
----------> %hash = %{$DataHash{$key}};

        # Get all the keys of that hash
        @hashKeys = keys %hash;

                    print "Data: ", $hash{ElementName}, "\n";

        #Get the items Programs that has been implemented
        @hashItems = @{$hash{ElementName}};

        #Try and match them up against the Task from Alfresco
        for($i = 0; $i < scalar @hashItems;$i++){
            for($j = 0; $j < scalar @promoCode; $j++){
                #Split up the PromoCode Here!!!! 0- File name, 1- Task #
                #If a match has been found
                if($hashItems[$i] ~~ $promoCode[$j]){
                    # Extract the row that match
                    foreach (@hashKeys){
                        @array = @{$hash{$_}};
                        #And store it in this array
                        push @pass, $array[$i];
                    }
                    # So that the information can be passed to the Reconcile routine to be added to the Details and Summary reports + Task #
                    &Reconcile(@pass,$i);

                    #Need insert and empty row
                    $sheet->Range("A$lastRow:G$LastRow")->Select;
                    $xl->Selection->Interior->{ColorIndex} = 15;
                    $xl->Selection->Interior->{Pattern} = xlSolid;
                }
            }
        }

    }
}'

Is there a problem with how i'm creating the hash of hash? How i'm reading it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-12-06 23:00:54

如果这是关于调试您的代码,我是否建议使用 Data::Dumper 来打印你的哈希值?它应该为您提供足够的信息来解决问题。

use Data::Dumper;
print Dumper \%hash;

If this is about debugging your code, might I suggest using Data::Dumper to print your hash? It should provide you with enough information to sort out the kinks.

use Data::Dumper;
print Dumper \%hash;
奢华的一滴泪 2024-12-06 23:00:54

一方面,这个语句:

$Data{$header[$counter]}  = @data;

将数组 @data 的大小分配给哈希值,可能不是您想要的!

要分配数组,您需要执行以下操作:

@{ $Data{$header[$counter]} } = @data

Well for one thing this statement:

$Data{$header[$counter]}  = @data;

will assign the size of the array @data to the hash value, probably not what you intended!

to assign the array you need to do this:

@{ $Data{$header[$counter]} } = @data
丢了幸福的猪 2024-12-06 23:00:54

这是不必要的:

foreach my $scalar(@$ref_array){
    push @header, $scalar;
}

因为它更简单地说:

push @header, @$ref_array;

但至于你的大问题,第 88 行(根据我的计数):

&Compare(\%DataHash);

因为 %DataHash 未定义 - 你可能没有 严格。这意味着它创建一个包变量 %main::DataHash,它是一个空哈希并传递对该空哈希的引用。因此它仅认为 $DataHash{ElementName} 将为 undef

由于您一直在处理 %Data,您可能想要这样做:

Compare( \%Data );

您不需要像调用 sub 那样使用 & 符号。

所以这是 USUW - use strict;使用警告;

This is needless:

foreach my $scalar(@$ref_array){
    push @header, $scalar;
}

as it is more simply stated:

push @header, @$ref_array;

But as for your big problem, line 88 (by my count):

&Compare(\%DataHash);

Because %DataHash is not defined--you probably don't have strict on. Which means that it creates a package variable %main::DataHash, which is an empty hash and passes a reference to that empty hash. So it only figures that $DataHash{ElementName} would be undef.

Since you've been dealing with %Data, you probably wanted to do this:

Compare( \%Data );

You don't need the ampersands the way you're calling the sub.

So this is a USUW - use strict; use warnings;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文