in reply to Bug with "last successfully matched regular expression" (empty regex)

I also could not get this !// thing to work in the grep. I am mystified why it doesn't, but I did recode according to Flipin good, or a total flop? which tests the value of the flip-flop regex and this does work in a grep. Just a point of reference:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @list= ("a".."c","DBIC","A".."C","DANCER","a".."c"); my @result; for (@list) { push @result,$_ if /DBIC/.. /DANCER/ and ! // } print Dumper \@result; # uses http://www.perlmonks.org/?node_id=525392 my @result2 = grep { (/DBIC/.. /DANCER/) =~ /^\d+(?<!^1)$/ }@list; print Dumper \@result2; __END__ $VAR1 = [ 'A', 'B', 'C' ]; $VAR1 = [ 'A', 'B', 'C' ];
The value of the flip-flop regex returns a line number like: 1,2,3,4E0 and that number can be tested in another regex.

As another note this "xE0" notation is used in other places in Perl. For example, the DBI can return 0E0 as the number of results which is 0 * 10**0 or numeric zero (0 * 1) although this evaluates to "true" when tested in a logical sense, meaning that that the statement "worked" but returned no results (numeric value is zero). Here the line number ending in E0 is the "last one". This is clever but it works.

  • Comment on Re: Bug with "last successfully matched regular expression" (empty regex)
  • Download Code

Replies are listed 'Best First'.
Re^2: Bug with "last successfully matched regular expression" (empty regex)
by LanX (Saint) on Feb 22, 2012 at 15:04 UTC
    Good catch, but the whole implementation is annoyingly inconsistent.

    I thought I could make this more obvious by implementing functions like nodege(), noflip(), and noflop() to write something like

    grep { noedge(/DBIC/ .. /DANCER/)  }@list;

    but try guessing which magic behavior hinders that:

    DB<52> @list= ("a".."c","DBIC","A".."C","DANCER","a".."c"); DB<53> sub ff { print $_[0]} DB<54> grep { ff(/DBIC/ .. /DANCER/) } @list; 0000000000 DB<55> grep { my $x=(/DBIC/ .. /DANCER/); ff($x) } @list; 12345E0

    Of course I could also use something like compiled regexes for EDGE, FLIP, FLOP to be able to type something like:

    grep { (/DBIC/ .. /DANCER/) !~ EDGE  } @list;

    but that doesn't seem to be trivial, too :-(

    Thanks anyway!

    Cheers Rolf

      ARGH!!!!!!!!!

      DB<52> @list= ("a".."c","DBIC","A".."C","DANCER","a".."c"); DB<53> sub ff { print $_[0]} DB<54> grep { ff(/DBIC/ .. /DANCER/) } @list; 0000000000 DB<55> grep { my $x=(/DBIC/ .. /DANCER/); ff($x) } @list; 12345E0

      ff() evaluates the range operator in list context!

      applying prototypes solves this "problem":

      DB<182> @list= ("a".."c","DBIC","A".."C","DANCER","a".."c"); DB<183> sub ff ($) { print $_[0]} DB<184> grep { ff(/DBIC/ .. /DANCER/) } @list; 12345E0

      Cheers Rolf ( ...banging his head against the table...)

        Sorry, I thought your "guess which magic" was "an exercise for the reader" and that you already knew the answer or else I would've told you that when you posted.

        - tye        

        You can also use scalar to force scalar context:
        grep { ff(scalar(/DBIC/ .. /DANCER/)) } @list;
Re^2: Bug with "last successfully matched regular expression" (empty regex)
by ikegami (Patriarch) on Feb 22, 2012 at 17:06 UTC
    That doesn't work if DBIC appears multiple times (which it can).