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

I am trying to print "$var1==$var2\n"; on one line in a text file but when I run this code it puts it on two lines and I couldnt find what I would put in there to print it all on one line. If someone could help me, I would greatly apreciate it. thanks
open (file, ">>myfile.txt"); print file "$var1==$var2\n"; close (file);

Replies are listed 'Best First'.
Re: output to a single line
by pg (Canon) on Jan 19, 2004 at 01:58 UTC

    Most likely, your $var1 and/or $var2 are not clean. Somewhere there is(are) returns.

    This helps to visually inspect your strings: (just pass your $var1, $var2 as parameters)

    use strict; use warnings; inspect("abcd\n123"); sub inspect { (my $copy = shift) =~ s/([^[:print:]])/sprintf " (0x%02x) ", ord $ +1/ge; print $copy; }

    If your $var1 and $var2 are read from terminal or a file, you might want to chomp them.

      Ooh, very slick :-). In cases like this, I just throw some < and > characters around a string.

      Arjen

Re: output to a single line
by crabbdean (Pilgrim) on Jan 19, 2004 at 02:16 UTC
    Hi Eva,

    What you don't tell us here is what $var1 and $var2 are for you. My first thought was maybe these variables are assigned text with a return character on the end, which is a common mistake when reading the $var1 or $var2 from user input or a from a file.

    Try "chomp"ing the file like this below. That will remove any return characters from the end of the variables, only if the exist.

    I had no problems getting it onto one line in the output file from the code you gave us. As you can see I haven't amended your code.

    Dean
    #!perl $var1 = "Dean\n"; chomp $var1; $var2 = "Eva\n"; chomp $var2; open (file, ">>myfile.txt"); print file "$var1==$var2\n"; close (file);
Re: output to a single line
by l3nz (Friar) on Jan 19, 2004 at 09:49 UTC
    Everybody has his own pet for this, but I'll go for this solution:
    print file "'$var1'=='$var2'\n";
    So that you know where your extra \n character is. I guess that if you see the expression on two lines, it should be in $var1, but you should try and see where the expression is broken.

    In any case, you'd better be aware that when you read a text file, you also read its embedded \n's, so you'd better chomp the lines before processing them.

Re: output to a single line
by davido (Cardinal) on Jan 19, 2004 at 03:57 UTC
    As others have pointed out, it is most likely that either $var1 or $var2 (or both) already contain a "\n" newline character which is causing an unwanted linebreak in the output. You may need to chomp them, or to pass them through a $var1 =~ tr/\n//d; filter to strip newlines. However, maybe you don't really want to completely and blindly strip newlines out of your data. You have to consider how it might somehow corrupt your data to blindly strip newlines without replacing them with some other delimiter.

    At minimum, perhaps you want to substitute mid-string newlines with a space character so that you don't have run-on words. Do that like this:

    chomp $var1; # Strip trailing newlines. $var1 =~ s/\n(?=.)/ /g; # Any newline that isn't at the end # of a string gets changed to one space.


    Dave

Re: output to a single line
by bart (Canon) on Jan 19, 2004 at 18:07 UTC
    Everybody here seems to be focussing on the newlines... which is nice, but depending on your platform (you dn't mention that), it might just as well be the carriage return character, "\r", that trips you up, instead of the newline "\n". On Unix, the visible effect would just be that the second half of a string starts again on the far left of your screen, but on Windows, some helpful text editors actually treat this the same as a newline ("\n").

    The way to get rid of them is very much alike to the way to get rid of unwanted embedded "\n" characters.

    p.s. I was not trying to be sarcastic, text editors that do that really are helpful, as they try to deal with platform-foreign text files in a nice manner.