#! perl use Modern::Perl; my $data; #load the data my $last_date ; while (){ my ($date, $type, $word) = split(/\s+\|\s+/, $_); chomp $word; # Check for a new date - base state if( $last_date and $date ne $last_date ){ print_out( $last_date, $data ); $data = {}; } if( exists $data->{$type} ){ $data->{$type} .= ', ' . $word; }else{ $data->{$type} = $word; } $last_date = $date; } # Pick up the last line print_out( $last_date, $data ); sub print_out{ my ( $date, $data ) = @_; print $date . ' | ' . ($data->{woody} // '') . ' | ' . ($data->{tinny} // '') . "\n"; } __DATA__ 28 Sep (Fri) | woody | caribou 28 Sep (Fri) | tinny | litterbin 29 Sep (Sat) | woody | wasp 29 Sep (Sat) | woody | yowling 29 Sep (Sat) | woody | gorn 30 Sep (Sun) | woody | intercourse 30 Sep (Sun) | woody | bound 30 Sep (Sun) | woody | pert 30 Sep (Sun) | tinny | newspaper 01 Oct (Mon) | woody | ocelot 01 Oct (Mon) | woody | concubine 01 Oct (Mon) | tinny | antelope 02 Oct (Tue) | woody | vole 02 Oct (Tue) | woody | sausage 03 Oct (Wed) | tinny | recidivist 03 Oct (Wed) | tinny | tit #### 28 Sep (Fri) | caribou | litterbin 29 Sep (Sat) | wasp, yowling, gorn | 30 Sep (Sun) | intercourse, bound, pert | newspaper 01 Oct (Mon) | ocelot, concubine | antelope 02 Oct (Tue) | vole, sausage | 03 Oct (Wed) | | recidivist, tit