为什么我得到一个未初始化的变量"错误

时间:2013-12-13 20:02:51

标签: regex perl split

只是想知道我的代码可能出错了以获得错误。我一直在 错误代码是:

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

我打印出我的阵列,但它们都打印出来,所以我有点不知道为什么我的变量是空的。谢谢!

#!/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;

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

1 个答案:

答案 0 :(得分:5)

问题在于:

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 - 字段。对于$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}) {
...

所以{I} $value就是undef哈希只能使用字符串作为键,因此undef被强制转换为空字符串'',并发出警告。

如何纠正?我不确定,因为我不确定您的代码的意图(我不知道您的输入数据将如何显示,以及您想要的输出)。通常,您需要将变量声明为尽可能接近其使用,而不是覆盖它们。我可以想象你的确想要:

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

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

相关问题