I am trying to make use of the Text::CSV Perl module to be able to parse a tab delimited file.
The file I am trying to parse is:
#IGNORE COLUMN1 COLUMN2 COLUMN3 COLUMN4
ROW1 x y z a
ROW2 b c d
ROW3 w
Note that the file is tab delimited. This file may have N columns and N rows. Also, in the case of ROW2, it has a fourth tab but no value. ROW3 has no tabs after the w value for COLUMN1. I.e. some columns may have undefined values or blank values.
So far, I have began writing a Perl script but have stumbled very early on in trying to figure out how I can write code to answer the following question:
Find out how many ROWn there are. Then for each COLUMNn check to see if I have ROWn values. So in this case, COLUMN2,COLUMN3 and COLUMN4 would have missing values.
Any tips and guidance would help (I'm new to Perl). I've looked at the CPAN Text::CSV page but I've not managed to be able to solve this problem.
#!/usr/bin/perl
use warnings;
use strict;
use v5.12;
use Text::CSV;
my $csv = Text::CSV->new ({
escape_char => '"',
sep_char => '\t',
eol => $\,
binary => 1,
blank_is_undef => 1,
empty_is_undef => 1,
});
open (my $file, "<", "tabfile.txt") or die "cannot open: $!";
while (my $row = $csv->getline ($file)) {
say @$row[0];
}
close($file);
ROW2,COLUMN4has an "empty" tab value or the line may end prematurely like it does inROW3where there is NO character after thewinCOLUMN1- user2402135sep_char => "\t"as'\t'makes the separator literally\tand not the tab character. - Kenosis$row? That you do not know how to compare empty and undefined? - TLP