Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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
Re: Match nothing in grep { ! // }
by McA (Curate) on Mar 14, 2013 at 06:28 UTC
    my @p = grep { defined $_ && $_ ne '' } @arr;

    McA

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: ""
      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?

      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.

Re: Match nothing in grep { ! // }
by choroba (Abbot) 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.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

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 pondering the Monastery: (10)
As of 2014-09-22 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls