#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use List::Util qw(uniq); my %hash; while () { my ($animal, $part) = split /\s+/; push @{$hash{$animal}},$part; } @{$hash{$_}} = uniq @{$hash{$_}} for keys %hash; print Dumper \%hash; __DATA__ bird beak bird beak bird claw bird wings bird feathers snake fangs snake scales snake fangs snake tail