Perl text::csv to check then add field -


I am reading a CSV file and checking text within a certain area. If text is present then I would like to keep the line and add one field.

In the following programs, the loop is fine, so that the lines can be saved with the text in the kernel [17]:

  while (my $ line = $ csv - & gt; getline ($ infh)) {if ($ row-> [16] "") {my $ string = $ csv-> String; Print {$ outfh} $ string; }}  

Loop also works fine in the following fields in another testing program, to add field:

  while (my $ line = $ Csv-> gt; match ($ inf)) {share @ $ line, 1, 0, "1 GM"; $ Csv-> Print ($ outfh, $ line); }  

I want to combine them to work in the same program, but I am doing Syntax Nightsemers. Can anyone show some signals to a Pearl novice?

There is a complete "test program" here:

  #! Use / usr / bin / perl strict; Use warnings; Text: Use CSV; Use CWD; File :: Use the basinum; My $ dirname = basename (getcwd); My $ input_file = $ dirname ".text"; My $ output_file = $ dirname '_extract.txt'; My $ csv = Text :: CSV- & gt; New ({Binary = & gt; 1, EOL = & gt; "\ 015 \ 012"}) or "can not use CSV":. Text :: CSV-> Error_deg (); $ INF, $ input_file or open to die '$ input_file' can not open: $! "Open your $ outfit, '' gt; & gt; ', $ output_file or die' can not open '$ output_file': $!"; While (my $ line = $ CSV-> Match ($ inf)) {Share @ $ line, 1, 0, "1 GM"; $ Csv-> Print ($ outfh, $ line); } $ Infh; About $ outfit; $ Csv-> EOF or die "processing '$ input_file' timed out before time '";  

Added: I would prefer a test program to include the test program testing;

  if ($ row-> [16]] "")  

from other programs - so much that added additional columns only to those rows Which I want to keep. How can this be understood ...

Added 2/18: It worked! , but do not look pretty ...

  #! Use / usr / bin / perl strict; Use warnings; Text: Use CSV; Use CWD; File :: Use the basinum; My $ dirname = basename (getcwd); My $ input_file = $ dirname ".text"; My $ output_file = $ dirname '_extract.txt'; My $ column = '1 GM'; # If a match is in my $ csv = Text :: CSV- & gt; New ({binary = & gt; 1, eol = & gt; "\ 015 \ 012"}) or die "CSV can not be used:. Text: CSV-> Error_Diag (); $ INF, $ Can not open 'input_file' or open '$ input_file' to die: $! "; Open your $ outfit, click '& gt; & Gt; ', $ output_file or die' can not open '$ output_file': $! "([$] - ($ $ Pankti- & gt; [16] the" ") # Add / insertion column if the call [17], the text {divide is @ $ row, while my $ row = $ Sisvi- & gt; Milan ($ INF)) 1, 0, "$" column; if ($ Pankti- & gt; [1] eq "$ column") # print new column {$ csv- & gt; print ( $ outfh, only to rows file with $ line);}}} close $ infh; about $ Autfh; $ Csv- & gt; Iof or die "processing" $ Input_fail "ended" prematurely;  

Added 2/18 5:40 pm - got it .. This works!

 < code> while (my $ line = gets $ csv- & gt; ($ Infh)) {splice @ $ row, 1, 0, "$ column"; # Add column if ($ Pankti- & gt; [17] the "") # print that test true {$ csv- & gt; print ($ outfh, $ line);}}  


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -