kwn has asked for the wisdom of the Perl Monks concerning the following question:

Howdy, Im trying to do something with all the contents of test.txt that starts with
a specific point in the text and ends with another specific point in
the text called #end. Right now this code wont return any
errors or warnings or anything at all actually... I messed
it up somehow... There are multiple #end strings but it should go to
the first one it sees from the start so it shouldnt matter right?
#!/usr/bin/perl use strict; use warnings; open("test", "<test.txt"); my @array = <test>; my @ex = grep(!/^####/, @array); my @test2 = grep(/#bg\sas,([a-z0-9])#end/, @ex); print @test2; close("test"); __DATA__ ####---------------------k ####---------------------l ####---------------k #bg at,super spaz razz tazz kazz #end #bg as,clock brock spock mock sock #end #bf as,lost fluff muff tuff cuff #end #bf aa,sauce dock spork fork torque #end ####-------------d-d ####------g-d

Replies are listed 'Best First'.
Re: matching between characters
by ikegami (Patriarch) on Oct 17, 2008 at 06:25 UTC

    Three reasons why your pattern won't match ever:

    • [...] matches a single character but you expect it to match more than that.
    • Your don't match newlines anywhere, but there are clearly some present between "#bg" and "#end".
    • No item of the grepped list contains both "#bg" and "#end".
Re: matching between characters
by Anonymous Monk on Oct 17, 2008 at 05:57 UTC
    Pay close attention
    #!/usr/bin/perl -- use strict; use warnings; open( TEST, "<test.txt") or die "CAN'T READ test.txt : $!"; while(<TEST>){ if( /#bg\sas/ .. /#end/ ) { print } } close TEST or die "CAN'T CLOSE test.txt : $!"; __END__ #bg as,clock brock spock mock sock #end
      Thanks guys, the example and the reference helped me ALOT!