Problem with unwanted newline

Discussion in 'Perl Misc' started by Andrew R. Gillett, Aug 6, 2003.

  1. Being a Perl newbie, I may be doing something very simple wrong here. The
    following line doesn't seem to work as I expect it to:

    print FILEHANDLE 'Thing:\n' . $var1 . '\n' . $var2;


    The desired output (if var1 = Hello and var2 = Goodbye) is:

    Thing:\nHello\nGoodbye


    But what I actually get is this:

    Thing:\nHello
    \nGoodbye


    The most likely explanation is that var1 has a newline in it (the text is
    read in from a file). I tried stripping \n from the variable, it didn't
    work but I'm not sure if this is the right way to do it:

    $var1 =~ s/"\n"//;


    --
    Andrew Gillett http://argnet.fatal-design.com/

    UK videogame release dates at:
    http://www.release-dates.co.uk/
     
    Andrew R. Gillett, Aug 6, 2003
    #1
    1. Advertising

  2. Andrew R. Gillett

    gnarred Guest

    You can write the first line as:

    print FILEHANDLE "Thing:\\n$var1\\n$var2";

    Note that \\ tells perl "I want a real slash, I don't want to escape a
    character", and the quotes "" will automatically expand variables
    contained within (but you already knew that)

    You're right, $var1 seems to have a newline at it's end, so you can:

    chomp($var1); # nix the newline

    Your regular expression doesn't work because perl treats the quote in
    the regex literally. Your regex will match a quote followed by a
    newline followed by a quote. You should write it as:

    s/\n//;

    A regular expression expands variables & escapees just like double
    quotes do. So, with your vars below:

    $str =~ s/$var1/$var2/;

    Will replace the first occurance of Hello in $str with Goodbye


    gnarred


    Andrew R. Gillett <> wrote in message news:<>...
    > Being a Perl newbie, I may be doing something very simple wrong here. The
    > following line doesn't seem to work as I expect it to:
    >
    > print FILEHANDLE 'Thing:\n' . $var1 . '\n' . $var2;
    >
    >
    > The desired output (if var1 = Hello and var2 = Goodbye) is:
    >
    > Thing:\nHello\nGoodbye
    >
    >
    > But what I actually get is this:
    >
    > Thing:\nHello
    > \nGoodbye
    >
    >
    > The most likely explanation is that var1 has a newline in it (the text is
    > read in from a file). I tried stripping \n from the variable, it didn't
    > work but I'm not sure if this is the right way to do it:
    >
    > $var1 =~ s/"\n"//;
     
    gnarred, Aug 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sukh
    Replies:
    1
    Views:
    401
    =?Utf-8?B?RFdT?=
    Mar 3, 2006
  2. Dragos Chirila

    NEWLINE character problem

    Dragos Chirila, Jan 30, 2004, in forum: Python
    Replies:
    1
    Views:
    6,937
    Nuff Said
    Jan 30, 2004
  3. John Nagle
    Replies:
    1
    Views:
    537
    John J. Lee
    Dec 27, 2006
  4. kelvinyung

    problem: unwanted latches inferred

    kelvinyung, May 3, 2009, in forum: VHDL
    Replies:
    2
    Views:
    1,234
    kelvinyung
    Jun 23, 2009
  5. Suzie Wilks

    Unwanted newline or eol between fields

    Suzie Wilks, Mar 5, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    97
    Paul Lalli
    Mar 5, 2004
Loading...

Share This Page