0

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

  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

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

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

4

1 回答 1

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.
}
于 2013-06-26T00:15:45.603 回答