2
问候语我试图按照本教程将文件读入散列哈希。
http://docstore.mik.ua/orelly/perl/prog3/ch09_04.htm
我的文字输入文件
event_a1_x1: [email protected] [email protected] email1_cnt=3
event_a1_x2: [email protected] [email protected] email1_cnt=3
event_b2_y1: [email protected] [email protected] email1_cnt=3
event_b2_y2: [email protected] [email protected] email1_cnt=3
event_c3_z1: [email protected] [email protected] email1_cnt=3
event_c3_z2: [email protected] [email protected] email1_cnt=3
我的代码是
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0] or die "Need to get config file on the command line\n";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
my %HoH;
#open FILE, "filename.txt" or die $!;
my $key;
my $value;
my $who;
my $rec;
my $field;
while (my $line = <$data>) {
print $line;
next unless (s/^(.*?):\s*//);
$who = $1;
#print $who;
$rec = {};
$HoH{$who} = $rec;
for $field (split) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
}
我不断收到这个错误...
Use of uninitialized value $_ in substitution (s///) at ./read_config.pl line 18, <$data> line 1.
您已分配给'$ line',因此您不能再使用'$ _'。所以'下一个除非$ line =〜s /...//',并且'split line'在下面的'$ line'。或者,删除'my $ line'(和所有其他'$ line's,只是'print;')。继续阅读好书,这段代码可以改进很多。 – zdim