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"; }