1

格納された参照 (extract Tripwire/get Data サブルーチンで設定) を参照し、(Compare サブルーチンで) ハッシュに変換しようとすると、つまり %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;
                }
            }
        }

    }
}'

ハッシュのハッシュを作成する方法に問題はありますか? どのように私はそれを読んでいますか?

4

3 に答える 3

2

これがコードのデバッグに関するものである場合、Data::Dumperを使用してハッシュを出力することをお勧めしますか? ねじれを整理するのに十分な情報を提供する必要があります。

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

一つには、この声明:

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

配列 @data のサイズをハッシュ値に割り当てますが、おそらく意図したものではありません!

これを行う必要がある配列を割り当てるには:

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

これは不要です:

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

それはより簡単に述べられているように:

push @header, @$ref_array;

しかし、あなたの大きな問題については、88行目(私のカウントによる):

&Compare(\%DataHash);

は定義されていないため%DataHash、おそらく on を持っていませんstrict。つまり%main::DataHash、空のハッシュであるパッケージ変数を作成し、その空のハッシュへの参照を渡します。したがって、それ$DataHash{ElementName}は となるだけundefです。

を扱ってきたので%Data、おそらくこれをやりたかったでしょう:

Compare( \%Data );

サブを呼び出す方法でアンパサンドは必要ありません。

これはUSUWです -use strict; use warnings;

于 2011-09-27T13:24:46.170 に答える