foreach my $line (@teststr) { my ($key, $val); $val = (split /\//, $line)[-1]; ($key = $val) =~ s/\.txt//; $testhash{$key} = $val; }