Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2002
    Location
    NJ
    Posts
    139

    Unanswered: last character prints on a new line

    Windxp
    perl 5.8.7

    I wrote a sample code to add a letter at the end of each line. The problem is that the letter
    is added on the next line even though it is part of the line above. how do I make sure it prints
    at the end of the line and not at the begining of the next?


    Lxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    Lxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    Wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    Oxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxx
    Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
    OU

    Code:
    $out="c:/perl/files/output.txt";
    open OUT, ">>$out" or die "Cannot open $out for append :$!";
    &printfile;
    close OUT;
    
    sub printfile {
    
    	open(INFILE,  "C:/CONV/R/demo.err")   or die "Can't open file: $!";
    		
    	$cnt2=0;
    
    	while (<INFILE>) { 
    		
    		$_=~ tr/A-Z/a-z/;
    
    		@array=split//,$_;
    		$x="";
    		$cnt=0;
    		
    		@message=split//,"HELLO WORLD. HOW ARE YOU?";
    				
    		foreach $i (@array) {
    				
    			if ($i eq @array[-1]) {
    				
    				$x = $x.$i.@message[$cnt2];
    				$cnt2 = $cnt2 + 1;
    
    			} else {
    	
    				$x = $x.$i;
    			}
    
    		}
    
    		print OUT $x.@message[$cnt2] if @array[0] ne "*" and @message[$cnt2] ne "";
    	}
    
    	close INFILE;
    }

  2. #2
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    You need to chomp the line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •