Regex 为什么我会得到一个;“未初始化变量”;错误

Regex 为什么我会得到一个;“未初始化变量”;错误,regex,perl,split,Regex,Perl,Split,我只是想知道我的代码可能有什么地方出错。我一直在 错误代码为: 在C:\begperl/final.pl第136138167169行,第2006行(#1)的哈希元素中使用未初始化的值$value 我去打印我的数组,它们都打印出来了,所以我有点不明白为什么我的变量是空的。谢谢 #!/usr/bin/perl use strict; use warnings; use diagnostics; #opens txt file: read mode open MYFILE, '<', 'so

我只是想知道我的代码可能有什么地方出错。我一直在 错误代码为:

在C:\begperl/final.pl第136138167169行,第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;

#########################################################################
#/usr/bin/perl
严格使用;
使用警告;
使用诊断;
#打开txt文件:读取模式
打开MYFILE、、“Summary_Report.txt”或die$!;
我的@header;
我的$i=0;
我的$packet_size=0;
我的$start\u time=undef;
我的$end_时间;
我的@source_ip;
我的@source\u端口;
我的$src_端口;
我的@src_端口;
我的@dest_ip;
我的@dest_港;
我的$destination\u端口;
我的目的港;
而{
chomp;#打破新的界限
#将TCPDUMP中的信息片段分离到列表中
@页眉=拆分(/\s+/,$);
如果(/^\d+:\d+/){
##############################我不是###################################
#将第一个“行和时间”定义为“特殊”
如果(/^22:28/&&!已定义($start\u time)){
$start_time=$header[0];
#打印“$start\u time\n”复选框
}   
如果(/22:28/){
$end_time=$header[0];
}       
############################S O U R C E##################################
#从来源对ip的每个部分进行分类
@source_ip=split('\.',$header[2]);
#联合知识产权
$source_ip[$i]=加入“”,@source_ip[0..3];
#打印$source_ip[$i];
@source_port=split(“:”,$source_ip[4]);
$src_端口[$i]=$source_端口[0];
#########################是的,是的###########################
#从目的地对ip的每个部分进行分类
@dest_ip=split(“\”,$header[4]);
#联合知识产权
$dest_ip[$i]=加入“.”,@dest_ip[0..3];
#打印$dest_ip[$i];
@dest_port=split(“:”,$dest_ip[4]);
$destination_port[$i]=$dest_port[0];
#打印$destination_port[$i];
#############################L N G T H#################################
#-1表示长度
#正在将$header[-1]转换为“总长度”
$packet_size+=$header[-1];
#打印$packet\u大小;
$i++;
}
}
我的@total_timesplit;
my@s_timesplit=split(“:”,$start_time);
#打印@s_timesplit;
my@e_timesplit=split(“:”,$end_time);
#打印@e_timesplit;
对于$i(0..2){
$total_timesplit[$i]=$e_timesplit[$i]-$s_timesplit[$i];
#打印@total_timesplit;
}
#产生平均数据包大小
my$avg_length=$packet_size/$i;
#打印$avg_长度;
关闭MYFILE;
#########################D A T A S E C T I O N###########################
打开MYFILE,“问题在于:

my $i = 0;
while (<MYFILE>)
    @source_ip = split ('\.', $header[2]);
    $source_ip[$i] = join '.', @source_ip[0 .. 3];
    ...
    $i++;
}
对于
$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}) {
...
因此,在某一点上,
$value
undef
。散列只能使用字符串作为键,因此
undef
被强制为空字符串
'
,并发出警告

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

my @source_ip;
while (<MYFILE>)
    my @ip_parts = split ('\.', $header[2]);
    push @source_ip, join '.', @ip_parts[0 .. 3];
    ...
}
my@source\u ip;
而()
my@ip\u parts=split(“\”,$header[2]);
推送@source_ip,加入“.”,@ip_部分[0..3];
...
}
元素将元素追加到给定数组的末尾,因此不需要指定索引。为临时
@ip_部分使用不同的变量以及存储结果的数组可以更容易地避免错误–变量很便宜,因此无需节约使用

问题在于:

my $i = 0;
while (<MYFILE>)
    @source_ip = split ('\.', $header[2]);
    $source_ip[$i] = join '.', @source_ip[0 .. 3];
    ...
    $i++;
}
对于
$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}) {
...
因此,在某一点上,
$value
undef
。散列只能使用字符串作为键,因此
undef
被强制为空字符串
'
,并发出警告

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

my @source_ip;
while (<MYFILE>)
    my @ip_parts = split ('\.', $header[2]);
    push @source_ip, join '.', @ip_parts[0 .. 3];
    ...
}
my@source\u ip;
而()
my@ip\u parts=split(“\”,$header[2]);
推送@source_ip,加入“.”,@ip_部分[0..3];
...
}

元素将元素追加到给定数组的末尾,因此不需要指定索引。为临时
@ip_部分使用不同的变量以及存储结果的数组可以更容易地避免错误–变量很便宜,因此无需节约使用

请只发布代码的相关部分。请(a)包括准确的错误消息,以及行号;(b) 包括样本数据;(c) 研究如何创建SSCCE(),以便尽可能少地研究无关代码。不过,最重要的是,代码、数据和错误消息必须同步。只需发布即可。其他成员告诉我要把我的全部代码复制粘贴到这里,而现在你却告诉我不要这样做。我赢不了。你至少为不同的目的使用了4次
$value
(端口、IP地址,一次也不显示)。行号应该告诉您运行库声明的
$value
的哪个版本未初始化。我敢打赌,您正在迭代的数组中有一个未定义的值。但这只是猜测。如果其他会员让你跳下悬崖,你会吗?发布尽可能少的代码来演示您的问题。请只发布