1

当我尝试延迟存储的引用(在 extract Tripwire/get Data 子例程中设置)并将其转换回哈希(在 Compare 子例程中)时,即 %hash = %{$DataHash{$key}}; ,然后我尝试打印密钥。我遇到了这些问题:

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

在打印中使用未初始化的值 .... 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;
                }
            }
        }

    }
}'

我如何创建散列的散列有问题吗?我怎么读的?

4

3 回答 3

2

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

use Data::Dumper;
print Dumper \%hash;
于 2011-08-17T14:25:08.087 回答
0

这是不必要的:

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

更简单地说:

push @header, @$ref_array;

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

&Compare(\%DataHash);

因为%DataHash没有定义——你可能没有strict。这意味着它创建了一个包变量%main::DataHash,它是一个空散列并传递对该空散列的引用。所以它只是$DataHash{ElementName}数字undef

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

Compare( \%Data );

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

所以这是一个USUW -use strict; use warnings;

于 2011-09-27T13:24:46.170 回答
0

一方面,这个声明:

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

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

要分配数组,您需要这样做:

@{ $Data{$header[$counter]} } = @data
于 2011-08-17T14:24:55.437 回答