#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub cleanString { my ( $str ) = @_; my ( $first , $second ) = split( / / , $str ); return substr( $second , 1 , length($second) -2 ); } my $File1 = 'file1.txt'; open (my $fh1, "<", $File1) or die "Error opening file1: $!\n"; my @keys; my $keyRef; while (<$fh1>) { chomp; next unless $_ ne ''; $keyRef->{$_} = 1; push @keys, $_; } close $fh1 or warn "Could not clode file1: $!\n"; # print Dumper $keyRef; # print Dumper \@keys; my $File2 = 'file2.txt'; open (my $fh2, "<", $File2) or die "Error opening file2: $!\n"; my %hash; while (<$fh2>) { chomp; next unless $_ ne ''; my @elements = sort( map { s/^\s+//; # strip leading spaces s/\s+$//; # strip trailing spaces $_ # return the modified string } split ';', $_ ); # print Dumper \@elements; $hash{cleanString($elements[0])} = cleanString($elements[1]); } close $fh2 or warn "Could not clode file1: $!\n"; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'ABS0059' => 'JOE', 'ABS0060' => 'MARY', 'ABS0057' => 'BILL', 'ABS0061' => 'STEPHAN', 'ABS0065' => 'RONIE', 'ABS0056' => 'SAM' };