Home » excel » Perl Excel parser truncating columns

Perl Excel parser truncating columns

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am new to Perl and trying to decode a Perl script which reads an Excel file and export the data into a text file. One of the steps in the script is performing weirdly.
This is the step:

@array_name = grep {$_} @array_name;

This step just truncates the last column if the value is 0; otherwise it works properly. If I remove this step, the last column with value 0 is back, but it includes some dummy NULL columns in my extract which are not part of my source Excel file.

Can someone please help me understand this step?

How to&Answers:

The grep command filters non empty content. So everything that does not evaluate as true will be discarded. Empty cells and cells containing 0 will be removed. If you just want to remove empty cells you could use

@array_name = grep { defined && length } @array_name ;

instead.

Answer:

$A->[$i][10]=~s/\n//g;

$variable_value =~s/\n//g;

$variable_value is the variable in which the value is stored.

It will remove the next line.

Answer:

grep basically returns all true values from the array, and 0 is interpolated as false.

For example grep is used here to filter true values into @b,

my @dirty = (0,1,2,3,,0,5,);
my @clean = grep {$_} @dirty;
for(@clean) {print "$_\n";}

Returns

1
2
3
5

A little trick for preserving the zeros involves adding a newline to the grep argument:

my @dirty = (0,1,2,3,,0,5,);
my @clean = grep {"$_\n"} @dirty;
for(@clean) {print "$_\n";}

Which returns,

0
1
2
3
0
5