http://www.perlmonks.org?node_id=906366


in reply to Re^2: using grep to search an array
in thread using grep to search an array

OK, so it is supposed to be a prefix. That means the \n at the end of $testit is getting in the way of letting it match.

Use chomp inside the loop.

Replies are listed 'Best First'.
Re^4: using grep to search an array
by alicatserver (Initiate) on May 23, 2011 at 21:11 UTC
    Chomp worked!! thanks
      I made an improvement to my script and I thought Id post it so that others can benefit from it.
      This rewritten version gives me two files... items that were "found" when searching the allper array and items that were "not found".
      (PS: the variables were renamed)

      foreach (@raw_data)
      {
      $item=$_;
      chomp($item);
      if (grep/$item/,@allper)
      {
      push(@found,grep/$item/,@allper);
      }
      else
      {
      push(@notfound,$item);
      }
      }

      open RESULT, ">_found_items.txt";
      print RESULT "@found\n\n";
      close(RESULT);

      open noRESULT, ">_doesnotexist.txt";
      print noRESULT "@notfound\n\n";
      close(noRESULT);