in reply to Conditional replace

Something like this?

my $add_at_end = 1; while( my $ln = <IN> ) { if( $ln =~ s/str1/str2/ && $add_at_end ) { $add_at_end = 0; } print OUT $ln; } print OUT "text" $add_at_end;

Replies are listed 'Best First'.
Re: Re: Conditional replace
by runrig (Abbot) on Aug 24, 2001 at 04:38 UTC
    Your last print has a typo :-) your logic is backwards :-) and its simpler IMO to say:
    my $add_at_end; while (<IN>) { $add_at_end = 1 unless s/str1/str2/; print OUT; } print OUT "text" if $add_at_end;
    Update:And I had a typo also. Fixed mine first :-)