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