Loading ...
Sorry, an error occurred while loading the content.
 

Grep - removing multiple array elements

Expand Messages
  • hooyar66
    I am having a problem with grep . I want to remove certain known elements from an array of strings - the code below is an example: #! perl use strict; use
    Message 1 of 2 , Aug 2, 2006
      I am having a problem with "grep". I want to remove certain known
      elements from an array of strings - the code below is an example:

      #! perl
      use strict;
      use warnings;
      use diagnostics;

      my @line = ("p1", " ", "Yes", "p2", "No", "Help!", "N/A");
      my @remove = ("Yes", "No", "N/A");

      @line = grep !/[@remove]/, @line;

      print @line;

      ----

      I would have expected the output to be "p1 p2Help" (note the
      whitespace), however what I am actually getting as output is "p1p2".

      I can't figure out how to get what I want, and I am confused why I
      get what I am getting.

      Any help is appreciated.
    • Charles K. Clarkson
      ... Character classes match single characters, not words. use Data::Dumper Dumper ; my @lines = ( p1 , , Yes , p2 , No , Help! , N/A ); my @remove =
      Message 2 of 2 , Aug 2, 2006
        hooyar66 wrote:

        : @line = grep !/[@remove]/, @line;

        Character classes match single characters, not words.


        use Data::Dumper 'Dumper';

        my @lines = ('p1', ' ', 'Yes', 'p2', 'No', 'Help!', 'N/A');

        my @remove = ('Yes', 'No', 'N/A');

        my $regex = join '|', @remove;

        @lines = grep !/$regex/, @lines;

        print Dumper \@lines;


        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        Free Market Advocate
        Web Programmer

        254 968-8328

        Don't tread on my bandwidth. Trim your posts.
      Your message has been successfully submitted and would be delivered to recipients shortly.