my @order = (); my %hash = (); while () { # split line on tab my @data = split /\t/; # for keeping the order push @order, $data[0] unless $hash{$data[0]}; # make an array ref if there isnt' one $hash{$data[0]} = [] unless $hash{$data[0]}; # store it away for later push @{$hash{$data[0]}}, $data[1]; } # keeping the order here for my $col1 (@order) { # get that stored array my $aref = $hash{$col1}; # print tab-delimited, grouped on col1 and in order print join("\t", $col1, @$aref), "\n"'; }