in reply to Re: Re: Finding Empty Columns
in thread Finding Empty Columns

So, for example, if column 1 is 0.0 in all rows, then don't print out that column?

Maybe this will do the trick: (assumes all rows have equal number of cells)
my @rows = map { chomp; [ split /;/ ] } <>; # now you have an array of arrays (rows of columns) my @non_zero_columns = grep { my $column = $_; grep { $_->[$column] != 0 } @rows } 0 .. $#{$rows[0]}; for ( @rows ) { print "@{$_}[ @non_zero_columns ]\n"; }
However, that's highly sub-optimal if there are few zero fields in the table.
This would be better:
my %zero_columns; @zero_columns{ 0 .. $#{$rows[0]} } = (); # initially, all columns mi +ght have 0. # eliminate any columns that have non-0 in any row: for ( my $r = 0; $r <= $#rows && keys %zero_columns; $r++ ) { for my $i ( keys %zero_columns ) { $rows[$r][$i] == 0 or delete $zero_columns{$i}; } } # invert the set: my @non_zero_columns = grep { ! exists $zero_columns{$_} } 0 .. $#{$rows[0]};

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.