use strict; my @t=qw(one two three); my @f=qw( one.ind.01 one.ind.02 one.tab.01 two.ind.01 two.tab.02 two.ind.02 three.ind.02 three.ind.01 three.tab.03 four1.ind four2.ind four2.ind f1v.ind.31 f1v.ind.32 f1v.ind.32 ); my %y; push( @{ $y{ ($a = $_, ( grep {$a=~/\b$_\.ind/} @t )[0] || next)[1] } }, $_ ) for @f;