in reply to while loop logic

Simpler to have a scalar which indicates which state you're in instead.

my $in_comment = undef; while( <CMDTXTF> ) { my @fld = split /\|/, $_; do { $in_comment = 1; next } if $fld[5] =~ m{ /\* }x; do { $in_comment = undef; next } if $fld[5] =~ m{ \*/ }x; print "$fld[0] $fld[2] sequence=$fld[4] $fld[5]" if /$regex/ and not $in_comment; }

Replies are listed 'Best First'.
Re^2: while loop logic
by Roy Johnson (Monsignor) on Jan 13, 2006 at 15:35 UTC
    This is exactly the sort of situation for which a flip-flop is intended:
    while( <CMDTXTF> ) { my @fld = split /\|/, $_; print "$fld[0] $fld[2] sequence=$fld[4] $fld[5]" if /$regex/ and not (($fld[5] =~ m{ /\* }x)..($fld[5] =~ m{ \*/ }x +)); }

    Caution: Contents may have been coded under pressure.
      I've tried out all the solution suggested in this post but there's still one problem I'm struggling to cope with. It transpires that whilst ignoring data between comments there are some cases where I need to run the regexp against what's left of the data in the record after the comments have been stripped e.g.
      asdfgh|kjkhg|poioiu|ytr|kkk|aaa /* vbfew */ kkkwwwqqqsss
      In this case the comment is complete within the record but I still need to check the rest of the record (aaa kkkwwwqqqsss) for any regexp matches.
      In fact this issue applies throught the data sets I'm trying to deal with. It was my mistake in not explaining this properly when asking for assistance. Which by the way has been excellent.

        This seems to handle both the scenarios you've outlined

        #! perl -slw use strict; while( <DATA> ) { chomp; if( m[/\*] ) { $_ .= <DATA> until m[\*/]; s[\s?/\* .+? \*/\s?][]smg; } print join '-', split '\|'; }

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.