use constant COMP => 0; use constant USER => 1; use constant UNKN => 2; my %inventory; open(my $fh_inv, '<', 'Inventory.csv') or die "Unable to open 'Inventory.csv' for reading: $!"; while ( <$fh_inv> ) { chomp; my ($key, $company, $user, $unkn) = (split /,/)[0, 3, 1, 4]; $inventory{$key} = [$company, $user, $unkn]; } my @Data; open(my $fh_clients, '<', 'clients.txt') or die "Unable to open 'clients.txt' for reading: $!"; while ( <$fh_clients> ) { chomp; my @field = split /,/; my $iref = $inventory{$field[3]}; if ( @$iref ) { push @Data, (join ',', $field[3], $iref->[COMP], @field[6,7,4,11], @{$iref}[USER, UNKN]); } } #processing @Data after this