#!/usr/bin/env perl use strict; use warnings; my %data; while () { chomp; next unless length; my ($id, $height) = (split)[0,3]; $height = ' ' unless defined $height; push @{$data{$id}}, $height; } my $format = "'%d', '%s',\t'%s'\n"; for my $id (sort { $a <=> $b } keys %data) { if ($data{$id}[0] eq ' ' && $data{$id}[1] eq ' ') { print "$id: both height markers missing - failed\n"; } else { printf $format, $id, @{$data{$id}}; } } __DATA__ 1 A A 6246 0.9706 1 B B 3237 0.9706 2 A 0 2 B B 5495 0.9775 3 A A 11254 0.9694 3 B 0 4 A 4 B