my %testhash = map { my $val = (split /\//, $_)[-1]; (my $key = $val) =~ s/\.txt//; $key => $val # <-- added } @teststr;