Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

G'day guitarplayer68,

The base problem is that when /line/ matches, the range operator becomes TRUE; thereafter, it never becomes FALSE. See perlop: Range Operators for a full description of the flip-flop boolean states of this operator.

The more specific problem (i.e. why the operator doesn't become FALSE), would be that /\\Z/ never matches. What that matches is a literal backslash (\\) followed by a capital zed (Z). I suspect that's not what you meant. I further suspect that you're perhaps confused with \Z which matches the end of a string (possibly before a newline if one exists). See Assertions under perlre: Regular Expressions. Unfortunately, \Z is not what you want either.

From the data you've shown, there doesn't appear to be anything you can use to match the last line in each file. Given this, you might be better off changing your code to something with logic along these lines:

#!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { $wanted_found = $line =~ /line/; } else { print $line; } } }

Output:

wantedA1 wantedA2 wantedB1 wantedB2 wantedC1 wantedC2

If you also wanted the line that matched /line/, you can change that logic to this:

#!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { next unless $wanted_found = $line =~ /line/; } print $line; } }

Output:

lineA wantedA1 wantedA2 lineB wantedB1 wantedB2 lineC wantedC1 wantedC2

-- Ken


In reply to Re: Regex returns match the first time. It returns everything thereafter by kcott
in thread Regex returns match the first time. It returns everything thereafter by guitarplayer68

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (7)
    As of 2014-12-26 09:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (170 votes), past polls