在Perl中,当我尝试初始化哈希时,为什么我会在“偶数大小列表预期”中找到“引用”?

时间:2016-08-03 13:44:18

标签: perl hash

我有一个脚本作为输入两个文件:

  • 提取200k行
  • 源文件

我必须将提取与源进行比较:提取的每一行都应该在源中。如果没有,我必须在日志文件中记录该行。

脚本下方:

#!/perl/bin/perl
use strict;
use warnings;
use feature 'say';
# Open log file
open(LOG, '>', 'log.txt');
# Start log
say LOG '['.localtime().'] Début execution';
# Declare extraction number of line counter
my $i_extraction_counter = 1;
# Define files to be compared (extraction against source)
my ($extraction_file, $source_file) = @ARGV;
# Open extraction file
open my $f_extraction, "<", $extraction_file;
# Store extraction file in a hash
my %h_extraction;
while(defined(my $extr_line = <$f_extraction>)){
    $h_extraction{$extr_line} = $extr_line;
    $i_extraction_counter++;
}
# Declare temp hash and counter
my %h_tmp = {};
my $i_counter = 1;
# Open source file
open my $f_source, "<", $source_file;
# For each source line, compare against extraction hash
while(defined(my $source_line = <$f_source>)){
    # If the current line exists in extration hash, stores it in the temp hash & increase counter
    if(defined($h_extraction{$source_line})){
        $h_tmp{$source_line} = $source_line;
        $i_counter++;
    }
    # TO DO : check in elsif if the line should be stored in log (name + firstname OR contract number)
}
# If not all lines of extraction has been stored in temp hash = some lines of extraction are not in source file
if($i_counter < $i_extraction_counter){
    # Declare a second temp hash (used to log missing lines)
    my %h_missing_lines = %h_extraction;
    # For each line of extraction, check if it exists in the first temp hash. If yes, unset it. Only missing line will remain
    foreach my $s_line (keys(%h_missing_lines)){
        if(defined($h_tmp{$s_line})){
            delete($h_missing_lines{$s_line});
        }
    }
    # For each line of missing lines hash, stores it in log file
    foreach my $s_line (keys(%h_missing_lines)){
        say LOG 'Ligne d\'extraction non présente dans fichier source : '.$s_line;
    }
}
say LOG '['.localtime().'] Fin execution';

执行时,此消息显示为:Reference found where even-sized list expected at script.pl line 22, <$f_extraction> line 200000.

1 个答案:

答案 0 :(得分:12)

在第22行,我们有:

class Test
{
    static void F() {
        D d = new D(__Method1);
    }
    static void __Method1() {
        Console.WriteLine("test");
    }
}

将其更改为:

Sub CompareLists()
Dim rng1 As Range, rng2 As Range
Dim cell As Range
Dim tmp As String

Set rng1 = Worksheets("Sheet1").Range("A1:A6")
Set rng2 = Worksheets("Sheet2").Range("A1:A6")

'Build pipe-delimited string from cells in range
For Each cell In rng1
    tmp = tmp & cell & "|"
Next cell

'Remove last pipe
tmp = Left(tmp, Len(tmp) - 1)

'Loop list 2 and compare against list 1.
'Specifically, see if each item in list 2 is found in list 1
For Each cell In rng2
    If InStr(1, tmp, cell) > 0 Then

        'Print items from list 2 that are found in list 1
        Debug.Print "Found: " & cell.Value

    Else

        'Print items from list 2 that are NOT found in list 1
        Debug.Print "NOT Found: " & cell.Value

    End If

Next cell

Set rng1 = Nothing
Set rng2 = Nothing
Set cell = Nothing

你会没事的。

这里的问题是 - my %h_tmp = {}; 用于定义匿名哈希 - 并返回引用。

所以你可以这样做:

my %h_tmp;

这就是你正在做的事情。

{}

使用像这样的单个值初始化散列,可以准确地给出错误,因为散列需要键值对。

你可以这样做:

my $hash_ref = {}; 

这会有效,但会多余 - 你无论如何都要创建一个空哈希。