#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use List::Util qw/uniq/; my $file = 'file.txt'; open( FILE, '<', $file ) or die $!; my %hash; while ( ) { chomp; my $lines = $_; my ($key, $value) = split(/ /, $lines); push @{ $hash{$key} }, $value; } foreach my $key( keys %hash ){ my @array = @{$hash{$key}}; my @uniq_elems = uniq @array; $hash{$key} = \@uniq_elems; } print Dumper(\%hash); exit;