in reply to Error using grep

Use a regex and add boundary conditions \b if you want them:
if (grep {/\Q$key\E/} @lines) {