2013-06-26 96 views
0

我想知道如果有人能幫助我更好地理解這個給定的代碼解析文本文件正在做什麼。Perl字符串解析代碼

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  [email protected] 
Doe, Jane 100 Well done! 0 Why didn't you do this? [email protected] 
Smith, Bob 0  0  [email protected] 

...將用於根據解析的文本設置格式化輸出。

我無法完全理解代碼塊如何解析並保存信息,以便我可以知道如何訪問我想要的信息的某些部分。有人能更好地解釋上述代碼在每個步驟中的作用嗎?

回答

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. 
} 
+0

謝謝你,所以這塊代碼沒有做更多的事情比改變一些文本的格式,並重新將它們與tab分隔值重新合併在一起正確嗎? – Yoink

+0

如果你把解釋*放在代碼裏面而不是註釋裏面 – doubleDown