#!/usr/bin/perl use strict; use warnings; my %uniq = (); while () { chomp; my ($foo, $bar) = split /\*/; $uniq{$bar} = $foo; } print "$_\n" foreach (keys %uniq); __DATA__ user1*house user2*nothing user3*house user4*house user5*other user6*nothing user7*main