Perl字符串解析代码

时间:2013-06-26 00:05:29

标签: string perl parsing text

我想知道是否有人可以帮助我更好地理解解析文本文件的代码是什么。

  while ($line = <STDIN>) {
    @flds = split("\t", $line);
    foreach $fld (@flds) {
        if ($fld =~ s/^"(.*)"$/\1/) {
            $fld =~ s/""/"/g;
        }
    }
    print join("\t", @flds), "\n";
}

我们将此代码块作为解析文本文件的开头,例如。

Name    Problem #1  Comments for P1 E.C. Problem    Comments    Email
Park, John  17  Really bad. 5       park@gmail.edu
Doe, Jane   100 Well done!  0   Why didn't you do this? doe2@gmail.edu
Smith, Bob  0       0       smith9999@gmail.com

...将用于根据解析的文本设置格式化输出。

我无法完全理解代码块如何解析和保存信息,以便我可以知道如何访问我想要的信息的某些部分。有人可以更好地解释上面的代码在每一步中做了什么吗?

1 个答案:

答案 0 :(得分:1)

这实际上看起来是解析CSV文件的一种非常糟糕的方式。

while ($line = <STDIN>) { #read from STDIN 1 line at a time.
    @flds = split("\t", $line);  #Split the line into an array using the tab character and assign to @flds
    foreach $fld (@flds) {  #Loop through each item/column that's in the array @fld and assign the value to $fld
        if ($fld =~ s/^"(.*)"$/\1/) {  #Does the column have a string that is surrounded in quotes?  If it does,  replace it with the string only.
            $fld =~ s/""/"/g; #Replace any strings that are only two double quotes.
        }
    }
    print join("\t", @flds), "\n";  #Join the string back together using the tab character and print it out.  Append a line break at the end.
}