in reply to Using Text::CSV

Win, I never used Text::CSV before, but it can't be that hard, right?

It installed fine for me just now using ppm:

C:\Documents and Settings\jnorton>ppm install Text::CSV Downloading ActiveState Package Repository packlist...not modified Downloading Text-CSV-1.13...done Unpacking Text-CSV-1.13...done Generating HTML for Text-CSV-1.13...done Updating files in site area...done 4 files installed

Even though your original post provides only a single line:

$colref = $csv->getline ($io);

(which you really cannot expect to be enough information, can you?!!), at least you provide an almost complete example by your 3rd post.  (I say "almost", because it still doesn't tell us what's in your data file "gridall.csv".  But no matter, we'll figure that out).

So let's try running your program.  The only changes I've made are removing some blank lines, consolodating the text in the pring statements, and adding use strict; and use warnings; (which you have been told about many times before, but still seem oblivous to the purpose for -- just because you can't see the errors in your program doesn't mean they're not there!):

use strict; use warnings; my $out = "Rows_of_interest.txt"; open (OUT, "+>$out"); open my $fh, "<", "gridall.csv" or die "gridall.csv: $!\n"; my $all_lines; my $matched_line_count; #my $csv; while (my $row = my $csv->getline($fh)) { $all_lines++; $row->[8] =~ m/^CE$/ or next; print OUT $row; $matched_line_count++; } $csv->eof or $csv->error_diag (); print "\n\nNumber of lines detected:\t$all_lines\n\n"; print "\n\nNumber of matched lines detected:\t$matched_line_content\n\ +n";

And sure enough, errors occur:

C:\>test Global symbol "$csv" requires explicit package name at C:\test.pl line + 21. Global symbol "$csv" requires explicit package name at C:\test.pl line + 21. Global symbol "$matched_line_content" requires explicit package name a +t C:\test. pl line 24. Execution of C:\test.pl aborted due to compilation errors.

So of course $csv, which is lexically-scoped to the while loop, isn't going to be defined on line 21:

$csv->eof or $csv->error_diag ();

Let's make a couple of changes:

First, let's actually use the module you need (!) with use Text::CSV.  Second, let's construct an actual Text::CSV object with my $csv = new Text::CSV;.  Third, let's initialize the variables $all_lines and $matched_line_count, so that if they're never initialized, we won't get warnings at the end.  And fourth, here's the actual data file "gridall.csv" that I'm going to use (which you really should supply to those of us that aren't mind-readers):

aa,bb,cc,dd,ee,ff,gg,hh,CE,ii,jk,ll

Here is the new program, and the results of running it:

use strict; use warnings; use Text::CSV; my $out = "Rows_of_interest.txt"; open (OUT, "+>$out"); open my $fh, "<", "gridall.csv" or die "gridall.csv: $!\n"; my $all_lines = 0; my $matched_line_count = 0; my $csv = new Text::CSV(); while (my $row = $csv->getline($fh)) { $all_lines++; $row->[8] =~ m/^CE$/ or next; print OUT $row; $matched_line_count++; } $csv->eof or $csv->error_diag (); print "\n\nNumber of lines detected:\t$all_lines\n\n"; print "\n\nNumber of matched lines detected:\t$matched_line_count\n\n" +; __END__ Output is: Number of lines detected: 1 Number of matched lines detected: 1

It took me a total of 10 minutes to get that, from installing Text::CSV with ppm, to fixing the program, because I put strict and warnings in, and fixed the errors they showed, as well as actually using the object, and creating an instance of it.

A couple of other comments:

  1. If you take the time to debug your output file "Rows_of_interest.txt", you'll see that it contains something like "ARRAY(0x185e85c)", which isn't very useful.  That's because you're printing $row to the file (Hint:  try dereferencing it first, or try using Data::Dumper on it:  use Data::Dumper;  print OUT Dumper($row), "\n")
  2. Even if you're new to Perl, you should get in the habit of using the 3-argument form of open, and get out of the habit of using bareword filehandles.  Read up on IO::File for more information.
  3. It's kind of overkill to use regexes like $row->[8] =~ m/^CE$/ or next; (which is the same as $row->[8] =~ /^CE$/ or next; anyway), when you can just use the equivalent:  $row->[8] eq "CE" or next;.

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/