use strict; use warnings; use diagnostics; use Data::Dumper; open(FIL,"report.txt") or die("$!"); my %k=(); while (my $line=) { next if $. < 2; my ($user,$roles,$type,$dt,$empty1,$empty2)=split(/\s*\|\s*/,$line); push @{$k{$user}{$type}}, $roles; } my @names = sort keys(%k); # Dumping the reference to the hash to more clearly show its structure print Dumper \%k; foreach ( @names ) { print "Name: ", $_, "\n"; if( exists($k{$_}{added}) ){ print "\tAdded:\n\t\t"; print join ',', @{$k{$_}{added}}; print "\n"; } if ( exists($k{$_}{deleted}) ) { print "\tDeleted:\n\t\t"; print join ',', @{$k{$_}{deleted}}; print "\n"; } }