foreach (@teststr) { my $val = (split /\//)[-1]; $testhash{substr $val, 0, index $val, '.txt'} = $val; }