sub searcharray { my ($terms, @array) = @_; ... my @matches = grep { $array[$_] =~ /$pattern/ } 0 .. $#array; @matches; } # call this with my @matches = searcharray($pss, @BKSIC_DESC);