Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Match nothing in grep { ! // }

by ag4ve (Monk)
on Mar 14, 2013 at 06:18 UTC ( #1023382=perlquestion: print w/ replies, xml ) Need Help??
ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

grep is filtering everything out when I don't provide an something. I'm getting around it by using a space (which will work as my data are urls but doesn't seem generally correct)

% perl -MData::Dumper -e 'my $i = ""; my @arr = qw/fe fi fo fum/; my +@p = grep { ! /$i/ } @arr; print Dumper(@p);' % perl -MData::Dumper -e 'my $i = " "; my @arr = qw/fe fi fo fum/; my + @p = grep { ! /$i/ } @arr; print Dumper(@p);' $VAR1 = 'fe'; $VAR2 = 'fi'; $VAR3 = 'fo'; $VAR4 = 'fum';

Comment on Match nothing in grep { ! // }
Download Code
Replies are listed 'Best First'.
Re: Match nothing in grep { ! // }
by choroba (Canon) on Mar 14, 2013 at 09:43 UTC
    Quoting perlop:
    The empty pattern //
    If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Match nothing in grep { ! // }
by Anonymous Monk on Mar 14, 2013 at 06:38 UTC

    Yeah , and then what? What is your question?

    The empty regex always matches

    $ perl -Mre=debug -e " m//" Compiling REx "" Final program: 1: NOTHING (2) 2: END (0) minlen 0 Matching REx "" against "" 0 <> <> | 1:NOTHING(2) 0 <> <> | 2:END(0) Match successful! Freeing REx: "" $ perl -Mre=debug -e " $_ = q/fi/; m//" Compiling REx "" Final program: 1: NOTHING (2) 2: END (0) minlen 0 Matching REx "" against "fi" 0 <> <fi> | 1:NOTHING(2) 0 <> <fi> | 2:END(0) Match successful! Freeing REx: ""
      The empty regex always matches
      Well, yes, and no. // is special, it serves as a shortcut, as if it's not the first regex you match, it signifies a repeat of the last regex that matched.

      perlreref:

      If 'pattern' is an empty string, the last successfully matched regex is used.

      In your tests here, there was no previous match, so this rule doesn't apply.

      I'm writing a scraper and sometimes I need to exclude some data. As I said, doing:

      $css->{xlink} //= ' ';
      works in this case but it just doesn't seem like the right way to do this.

      @{$ret->{link}} = grep { ! /$css->{xlink}/ } map { $_->{href} } $dom-> +find($css->{link})->each if ($css->{link});

        IMHO my post should have helped you as it showed that you can extend the expression in the grep codeblock.

        But now we know a little more of your problem and I assume the following: If $css->{xlink} is defined and not empty you want to try a match. The otherwise clause is not obvious to me, but you can change it. E.g.

        grep { $css->{xlink} && ! /$css->{xlink}/ }

        I hope I understood it right.

        McA

        I'm writing a scraper and sometimes I need to exclude some data. ...

        Well, that much (your desire) was clear from your original post, but its still not a question :)

        If you want to filter based on a regex, and a space isn't adequate, what is adequate?

        ?? if( sometingTrue ){ grep filter } else { no grep filter }

        Or were you after what McA wrote?

Re: Match nothing in grep { ! // }
by McA (Priest) on Mar 14, 2013 at 06:28 UTC
    my @p = grep { defined $_ && $_ ne '' } @arr;

    McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023382]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2015-07-31 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls