for (sort keys %data) { my @content = "$data{$_},$_"; if (grep(/.*,.*,.*,.*$/, @content)){ @column = grep(/.*,.*,.*,.*$/, @content) }; #@column = grep(/.*,.*,.*,.*$/, @content); print $fh_out "@column\n"; }