next if (exists $cache{$_}); or next if (grep /\b$_\b/ , @cache); and later on ..... push @cache , $_; or $cache{$_}++;