为什么我收到"未初始化的变量"错误

 丫头丫头520 发布于 2023-02-10 15:27

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

"在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 () { 
    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 () { 

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

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

amon.. 5

问题出在这里:

my $i = 0;
while ()
    @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}) {
...

所以有一点$valueundef.一个哈希只能使用字符串作为键,以便undef将强制使用空字符串'',并且会发出警告.

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

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

push内置附加元素给定数组的末尾,这样你就不需要指定索引.对临时@ip_parts和数组使用不同的变量来存储结果可以更容易地避免错误 - 变量很便宜,因此不需要谨慎使用它们!

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}) {
    ...
    

    所以有一点$valueundef.一个哈希只能使用字符串作为键,以便undef将强制使用空字符串'',并且会发出警告.

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

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

    push内置附加元素给定数组的末尾,这样你就不需要指定索引.对临时@ip_parts和数组使用不同的变量来存储结果可以更容易地避免错误 - 变量很便宜,因此不需要谨慎使用它们!

    2023-02-10 15:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有