open(IMGDB, "$img_db"); # Open db for reading @imgarray=; close (IMGDB); my @matchingOptions = grep{ /\Q$search\E/i }, @imgarray; foreach my $imgline (@matchingOption) { my ($img_name,$img_desc) = split /\|/, $imgline; print <$img_desc End_of_head } } ... for my $imgline ( @matchingOptions ) { # Do things; }