http://www.perlmonks.org?node_id=990965

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

hi perl monks!,....

please help me whether i am on the right track

i have an issue in handling line contiuation..

my line 1 is said to be continued to line 2 only when my 72nd column has '*'.

Things i am trying to do is

1) check whether 72nd column is *

2)chomp the newline character...

here, this situation occurs at any line in the code. and i want to retain the rest of the code as it is.

can anyone suggest me a better method?...

Thanks in advance...

Replies are listed 'Best First'.
Re: how to append 2 lines
by aitap (Curate) on Aug 31, 2012 at 10:10 UTC

      Hi,here is the sample input code...

      LABEL#00001 MVC SYMBOL+2(4),X.PDES_KEY+2 + MVC SYMB_ART,=C'XSEQ' //line comments + + F$SFH SELECT,LISTING, + * DESKART=SYMB_ART, + * DESKWERT=SYMBOL, + * MARKTCODE=L#MACF + BNE F03#000 //line comments

      my required output should be somewhat like this...

      LABEL#00001 MVC SYMBOL+2(4),X.PDES_KEY+2 + MVC SYMB_ART,=C'XSEQ' //line comments + + F$SFH SELECT,LISTING,DESKART=SYMB_ART,DESKWERT=SYMBOL,MARKTCO +DE=L#MACF BNE F03#000 //line comments

        And your Perl code to join the lines is...

        Hint: use three-argument form of substr to check the 72nd symbol of the line and use it again to replace the text. For example:

        my $one = ("_"x9)."*__\n"; my $two = ("-"x15)."\n"; print "$one$two"; substr($one,9)="" if substr($one,9,1) eq "*"; print "$one$two";

        Sorry if my advice was wrong.
        Ok, but that is only DATA, that is not perl
Re: how to append 2 lines
by philiprbrenan (Monk) on Sep 01, 2012 at 17:23 UTC

    Something along the lines of:

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; my ($c, $s) = (0, ''); for(split /\n/, << 'END') LABEL#00001 MVC SYMBOL+2(4),X.PDES_KEY+2 + MVC SYMB_ART,=C'XSEQ' //line comments + + F$SFH SELECT,LISTING, + * DESKART=SYMB_ART, + * DESKWERT=SYMBOL, + * MARKTCODE=L#MACF + BNE F03#000 //line comments END {if (length > 71 and substr($_, 71, 1) eq '*') # Detect continuation {s/\A\s*// if $c == 1; # Strip leading space +in continuation s/\s*\*\s*\Z//; # Strip trailing space ($s, $c) = (($c ? $s : '').$_, 1); # Concatenate or resta +rt depending on continuation state } else {s/\A\s*// if $c == 1; # Strip leading spaces + in continuation say "$s$_"; # Write completed stat +ement ($s, $c) = ('', 0); # Restart } }

    Produces

    LABEL#00001 MVC SYMBOL+2(4),X.PDES_KEY+2 + MVC SYMB_ART,=C'XSEQ' //line comments + + F$SFH SELECT,LISTING,DESKART=SYMB_ART,DESKWERT=SYMBOL,MARKTCO +DE=L#MACF BNE F03#000 //line comments
      Hi,....

      Thanks for the advice....

      but another doubt....This is fine if i give the input then and there... but i want to give my input in a file... i want to perform this action for the above lines in a file.. please help me in this also...

      Thanks in advance...

        May I suggest something like this to read your lines of text:

        use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); sub readFile($) {my ($f) = @_; $f or die "readFile: No file specified"; open my $F, "<$f" or die "Cannot open $f for input"; local $/ = undef; <$F> } print readFile("readFile.pm");

        Produces:

        use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); sub readFile($) {my ($f) = @_; $f or die "readFile: No file specified"; open my $F, "<$f" or die "Cannot open $f for input"; local $/ = undef; <$F> } print readFile("readFile.pm");