in reply to Re: Help with null string behavior in regex?
in thread Help with null string behavior in regex?

I see what this is doing, but I'm going to the shed with the Camel before I'll be comfortable maintaining it.

BTW, the kluge I used to solve this was
if ($stringFile eq "") { print "No file of exclude strings specified\n"; @strings = "xyzzy";

which is effective, but it makes me feel all dirty. =)

Replies are listed 'Best First'.
Re: Re: Re: Help with null string behavior in regex?
by diotalevi (Canon) on May 03, 2004 at 22:58 UTC

    I hoped that what I was doing was clear enough. The real improvements were doing a chomp() on the whole list at once, only once, removing any empty elements, then pre-compiling all the patterns.

    I also changed your for ( ... ) { unless ( ... ) { ...; last } } because it read strangely for me. unless() is like that, sometimes it just reads strangely. Since the intent of that code was to be an "everything matches or I print the file name" I just skipped right to the next pattern if there was a match and made the exception stand out.

      It's definitely clear! But "grep length" and "map qr" haven't made it into my syntactical bag yet.

      I get new stuff to read about every time I post code here.

      BTW, I didn't know "chomp @array" was possible. I posted a question recently trying to "lc @array", which *isn't* possible, so I just figured the same rule applied.

        Oh ok. This is easy enough.

        @foo = grep length(), @foo; # is equivalent to but faster and clearer than my @tmp; for my $element_of_foo ( @foo ) { if ( length( $element_of_foo ) > 0 ) { push @tmp, $element_of_foo; } } @foo = @tmp;

        The line @foo = map qr/$_/, @foo; could be omitted from your program with no effect except of being slower. You'll see notes on qr// in perlop. The entire point is to pre-compile each regular expression so that when you do tests against it later perl won't have to do everything on the fly (and everytime you test a new file).