user1415006
user1415006

Reputation: 51

Perl Excel parser truncating columns

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?

Upvotes: 3

Views: 492

Answers (3)

sang kaul
sang kaul

Reputation: 509

$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.

Upvotes: 1

John Dewey
John Dewey

Reputation: 7093

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

Upvotes: 0

dgw
dgw

Reputation: 13646

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.

Upvotes: 1

Related Questions