Perl Excel Truncator Columns

I am new to Perl and am trying to decode a Perl script that reads an Excel file and exports the data to a text file. One of the steps in the script is weird. This is the step:

@array_name = grep {$_} @array_name;

This step simply trims the last column if the value 0; otherwise it works correctly. If I delete this step, the last column with the value 0will return, but it includes several dummy columns NULLin my extraction that are not part of my original Excel file.

Can someone please help me understand this step?

+3
source share
3 answers

grep . , , . , 0, . ,

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

.

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

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

$variable_value - , .

.

+1

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

For example, grepused here to filter true values ​​in @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 to keep zeros involves adding a new line to the grep argument:

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

What returns

0
1
2
3
0
5
0
source

All Articles