in reply to Re: Re: Finding Empty Columns
in thread Finding Empty Columns
However, that's highly sub-optimal if there are few zero fields in the table.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"; }
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.
|
|---|