foreach $komp_dir(@komp_dir) { if (-d $komp_dir){ my @fields = split /\//, $komp_dir; $fields[6] =~ /(^\d*)_(\w*)$/ and print "$1\n"; } }