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

I have the following code that reads a text file. I want the output to be a single line. However it appears to wrap or otherwise place a new line int the output. Please look at the code, input file, and sample of the output, and tell me why this is happening.
Thanks in advance.
**************************************** #Open a file open(MYFILE,"c:\\perl\\Files\\zones2add.txt") || die("Cannot open file"); #open(MYOUT,">c:\\perl\\Files\\myout.txt"); while(<MYFILE>) { print "tdnscmd \. /recordadd $_ www a 12.39.123.78\n"; } # close the file close MYFILE; *********************************** Input file: Aedonstaffing.biz Aedonstaffing.info Aedonstaffing.net Aedonstaffing.org ************************** output tdnscmd . /recordadd Aedonstaffing.biz www a 12.39.123.78 tdnscmd . /recordadd Aedonstaffing.info www a 12.39.123.78 tdnscmd . /recordadd Aedonstaffing.net www a 12.39.123.78 tdnscmd . /recordadd Aedonstaffing.org www a 12.39.123.78 *********************************
I expected to get:
tdnscmd . /recordadd Aedonstaffing.org www a 12.39.123.78

Replies are listed 'Best First'.
Re: Perl Output
by pzbagel (Chaplain) on Apr 23, 2003 at 21:05 UTC
    try:
    while(<MYFILE>) { chomp(); print "tdnscmd \. /recordadd $_ www a 12.39.123.78\n"; }

    chomp() will remove the last whitespace character in a string including the \n that are plaguing you.

      Thanks that did exactly what I needed.
Re: Perl Output
by Abigail-II (Bishop) on Apr 23, 2003 at 21:32 UTC
    Well, you read in a line (into $_), don't modify it, interpolate it in a string that contains a newline, print that interpolated line, and then complain you are getting newlines in the output.

    First, lose the \n in the print command, or else you get a newline for each line you print. Second, lose the newline you read in, for instance, by using chomp.

    Abigail

      Thanks for your suggestion, however, in doing so the results are a wrapped line. I did not want to change the original line, I was simply adding some test to the front and back of it to make a complete command.
        Uhm, your posting says you wanted the output to be a single line. Removing all newlines from your output will make it a single line. If it's too long, many (pseudo)terminals will wrap it.

        Abigail