0

文件数据块的格式与以下文件相同

edit_file content{
       val0      data0
       val0      data0
       val0      data0
       val0      data0
       val0      data0
   }

我的代码是

my $temp_hash;
open FD, "<temp.cfg";
@array = <FD>;
foreach $line (@array) {
    if ($line =~ /\s+(.*?)\s+(.*)/) {
        foreach $key (keys %tem) {
            $temp_hash{$1} = $2;
        }
    }
    print $temp_hash;
}
foreach $array (keys $1) {
    print "$key is $temp_hash{$key}\n";
}
4

1 回答 1

0

如果您只想将文件解析为哈希:

my %data;
open my $fh, "<", "temp.cfg" or die $!;
while ( my $line = readline($fh) ) {

    if ( $line =~ m{^\s+(\S+)\s+(\S+)$} ) {

        $data{$1} = $2;
    }
}
close $fh;

foreach my $key (keys %data) {
    print "$key is $data{$key}\n";
}
于 2013-07-15T12:29:40.690 回答