【问题标题】:Why am I getting an "uninitialized variable" error为什么我收到“未初始化的变量”错误
【发布时间】:2013-12-14 04:43:18
【问题描述】:

只是想知道我的代码可能做错了什么以得到错误。我不断得到 错误代码是:

“在 C:\begperl/final.pl 第 136,138,167,169 行,第 2006 行 (#1) 的哈希元素中使用未初始化的值 $value”

我去打印我的数组,但它们都打印了,所以我对为什么我的变量为空感到有点茫然。谢谢!

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

#opens txt file: read mode
open MYFILE, '<', 'source_file.txt' or die $!;

#opens output txt file: write mode
open OUT, '>', 'Summary_Report.txt' or die $!;

my @header;

my $i = 0;
my $packet_size = 0;

my $start_time = undef;
my $end_time;

my @source_ip;
my @source_port;
my $src_port;
my @src_port;

my @dest_ip;
my @dest_port;
my $destination_port;
my @destination_port;

while (<MYFILE>) { 
    chomp; #break new line

    #separate pieces of information from TCPDUMP into list
    @header = split (/\s+/, $_);

    if (/^\d+:\d+/) {

##############################T I M E###################################

    #defining first 'line & time' as 'special'
    if (/^22:28/ && !defined($start_time)) {
        $start_time = $header[0];
        #print "$start_time\n"; check
    }   

    if (/22:28/) {
        $end_time = $header[0];
    }       

############################S O U R C E##################################

    #categorizing each section of ip's from source
    @source_ip = split ('\.', $header[2]);

    #joining ip's together
    $source_ip[$i] = join '.', @source_ip[0 .. 3];
    #print $source_ip[$i]; 

    @source_port = split (':', $source_ip[4]);
    $src_port[$i] = $source_port[0];

#########################D E S T I N A T I O N###########################

    #categorizing each section of ip's from destination
    @dest_ip = split ('\.', $header[4]);

    #joining ip's together
    $dest_ip[$i] = join '.', @dest_ip[0 .. 3];
    #print $dest_ip[$i]; 

    @dest_port = split (':', $dest_ip[4]);
    $destination_port[$i] = $dest_port[0];
    #print $destination_port[$i];

#############################L E N G T H#################################

    #-1 represents length
    #transferring $header[-1] into 'total length'
    $packet_size += $header[-1];
    #print $packet_size; 

    $i++;

    }
}

my @total_timesplit;

my @s_timesplit = split (':', $start_time);
#print @s_timesplit;

my @e_timesplit = split (':', $end_time);
#print @e_timesplit; 

for $i (0 .. 2) {
    $total_timesplit[$i] = $e_timesplit[$i] - $s_timesplit[$i];
    #print @total_timesplit;
}

#Yields average packet size
my $avg_length = $packet_size/$i;
#print $avg_length;

close MYFILE;

#########################D A T A S E C T I O N###########################

open MYFILE, '<', 'source_file.txt' or die $!;

my $user = 0;
my $pass = 0;

#separating loop to reset values#
while (<MYFILE>) { 

    #finds all instances of USER
    $user++ if /USER/i;
    #print $user;

    #finds all instances of PASS
    $pass++ if /PASS/i;
    #print $pass;

}

##############################SOURCEIPHASH##############################

my %seenip_source;
my @uniqueip_source;
my $sourceips_unique;

foreach my $value (@source_ip) {
    if (! $seenip_source{$value}) {
        push @uniqueip_source, $value;
        $seenip_source{$value} = 1;

    }
}
$sourceips_unique = @uniqueip_source;


#########################SOURCEPORTHASH#################################

my %seenport_source;
my @uniqueport_source;
my $sourceports_unique;

foreach my $value (@source_port) {
    if (! $seenport_source{$value}) {
        push @uniqueport_source, $value;
        $seenport_source{$value} = 1;
    }
}
$sourceports_unique = @uniqueport_source;

##########################DESTINATIONIPHASH#############################

my %seenip_dest;
my @uniqueip_dest;
my $destips_unique;

foreach my $value (@dest_ip) {
    if (! $seenip_dest{$value}) {
        push @uniqueip_dest, $value;
        $seenip_dest{$value} = 1;
    }
}
$destips_unique = @uniqueip_dest;

#########################DESTINATIONPORTSHASH###########################

my %seenport_dest;
my @uniqueport_dest;
my $destports_unique;

foreach my $value (@dest_port) {
    if (! $seenport_dest{$value}) {
        push @uniqueport_dest, $value;
        $seenport_dest{$value} = 1;
    }
}
$destports_unique = @uniqueport_dest;

#########################################################################

【问题讨论】:

  • 请只发布代码的相关部分。
  • 请 (a) 包括准确的错误信息,以及行号; (b) 包括样本数据; (c) 研究如何创建 SSCCE (Short, Self-Contained, Correct Example),以便尽可能少地研究无关代码。不过,最重要的是代码、数据和错误消息要同步。
  • 刚刚发布。其他成员告诉我将我的整个代码复制并粘贴到这里,现在你告诉我不要这样做。我赢不了。
  • 您至少使用了 4 次 $value 用于不同的目的(端口、IP 地址,也曾经取消使用过)。行号应该告诉您运行时声称哪个版本的$value 未初始化。我敢打赌,您正在迭代的数组之一中有一个未定义的值。但这是一个猜测。
  • 如果其他成员让你跳崖,你会吗?发布尽可能少的代码来证明您的问题。

标签: regex perl split


【解决方案1】:

问题出在这里:

my $i = 0;
while (<MYFILE>)
    @source_ip = split ('\.', $header[2]);
    $source_ip[$i] = join '.', @source_ip[0 .. 3];
    ...
    $i++;
}

split 行可能创建了四个元素,所以source_ip 类似于("111", "112", "113", "114")。您将这四个字段连接在一起并将它们写入 $i-th 字段。对于$i == 0,这会覆盖第一个字段:

("111.112.113.114", "112", "113", "114")

对于$i == 4,这会附加一个字段:

("111", "112", "113", "114", "111.112.113.114")

对于$i == 5 或更大,中间会有一个空字段:

("111", "112", "113", "114", undef, "111.112.113.114")

稍后,您循环遍历该数组:

foreach my $value (@source_ip) {
    if (! $seenip_source{$value}) {
...

所以在某一时刻$valueundefhash 只能使用字符串作为键,因此undef 被强制转换为空字符串'',并发出警告。

如何纠正?我不确定,因为我不确定你的代码的意图(我不知道你的输入数据会是什么样子,以及你想要什么输出)。通常,您会希望声明变量尽可能接近它们的用途,而不是覆盖它们。我可以想象你的实际意图:

my @source_ip;
while (<MYFILE>)
    my @ip_parts = split ('\.', $header[2]);
    push @source_ip, join '.', @ip_parts[0 .. 3];
    ...
}

push builtin 将元素附加到给定数组的末尾,因此您无需指定索引。为临时的@ip_parts 和存储结果的数组使用不同的变量可以更容易地避免错误——变量很便宜,所以没有必要谨慎使用它们!

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多