FAQ
Hi

I created a script that reads a file and attaches a string infront of each read in item. When the file is saved with the changes though, each item has a blank line between the next item. The original text document does is single sapced.

Original tesxt file
item1
item2
item3

Saved text file
string item1

string item2

string item3

I think what his happening is when the items are read in lots of white space is attached to the end of the string.

Is there a way to remove only trailing white space, from a string, but retain white spaces with in the string?
thanks,

Marc

Marc Fromm
Computer Systems Specialist
University of Montana--Western
710 South Atlantic Street
Dillon, MT 59725
406-683-7164



________________________________________________________________
Sent via the HotDawg Mail system at hotdawg.umwestern.edu

Search Discussions

  • Gary Stainburn at Feb 7, 2003 at 9:07 am

    On Friday 07 Feb 2003 5:04 am, Fromm, Marc wrote:
    Hi

    I created a script that reads a file and attaches a string infront of each
    read in item. When the file is saved with the changes though, each item
    has a blank line between the next item. The original text document does is
    single sapced.

    Original tesxt file
    item1
    item2
    item3

    Saved text file
    string item1

    string item2

    string item3

    I think what his happening is when the items are read in lots of white
    space is attached to the end of the string.

    Is there a way to remove only trailing white space, from a string, but
    retain white spaces with in the string? thanks,
    Hi Marc,

    Unless you're doing something peculiar, nothing will be added to the string as
    you read it in. However, it will contain the EOL character - on DOS it's
    CRLF, on unix it's LF and on mac's it CR.

    If you're then adding the EOL when you're printing it, then that's your
    problem. Either chomp the input or don't add a new one on output.

    If this doesn't help, try posting the code in question so we can have a look.

    HTH

    Gary

    Marc

    Marc Fromm
    Computer Systems Specialist
    University of Montana--Western
    710 South Atlantic Street
    Dillon, MT 59725
    406-683-7164



    ________________________________________________________________
    Sent via the HotDawg Mail system at hotdawg.umwestern.edu
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
  • R. Joseph Newton at Feb 8, 2003 at 5:26 am

    "Fromm, Marc" wrote:

    Hi

    I created a script that reads a file and attaches a string infront of each read in item. When the file is saved with the changes though, each item has a blank line between the next item. The original text document does is single sapced.

    Original tesxt file
    item1
    item2
    item3

    Saved text file
    string item1

    string item2

    string item3

    I think what his happening is when the items are read in lots of white space is attached to the end of the string.

    Is there a way to remove only trailing white space, from a string, but retain white spaces with in the string?
    thanks,

    Marc
    Hi Mark,

    The safest apraoch for such situations is to use chomp and then add a newline to each line in the output. The chomp function is a smart little bugger--it eats trailing newlines if they exist, but will otherwise leaves the string untouched.

    Of course, if you want your output to come out exactly as it was in the file, then just leave it alone and pass it through:

    while <FILE> {
    print $_;
    }

    Likewise, if you are manipulating the lines but not in ways that would truncate the newline at the end, you can just pass them through. That is a bit risky, though, so it's probably better to just chomp on input and restore the newline on output.

    Joseph
  • Gary Stainburn at Feb 10, 2003 at 9:24 am
    Hi Marc,

    Any chance of seeing some code/data?

    If you're chomping the input, and as output you have

    print $prefix,$data,"\n";
    or
    print "$prefix$data\n";

    then something must be happening to your $prefix value; either that, or maybe
    you've got another print statement somewhere. Maybe if your "\n" is in a
    string, something's getting appended to the string.

    Whichever t is, I can't help without seeing your code.


    BTW, allways CC follow-ups back to the list so other people can help.

    Gary
    On Friday 07 February 2003 11:02 pm, Fromm, Marc wrote:
    I tried the chomp and it produced:
    string item1
    string item2
    string item3

    It got rid of the spaces between itmes, but notice the incremental
    indention.

    Marc

    Marc Fromm
    Computer Systems Specialist
    University of Montana--Western
    710 South Atlantic Street
    Dillon, MT 59725
    406-683-7164




    ---------- Original Message ----------------------------------
    From: Gary Stainburn <gary.stainburn@ringways.co.uk>
    Date: Fri, 7 Feb 2003 09:00:23 +0000
    On Friday 07 Feb 2003 5:04 am, Fromm, Marc wrote:
    Hi

    I created a script that reads a file and attaches a string infront of
    each read in item. When the file is saved with the changes though, each
    item has a blank line between the next item. The original text document
    does is single sapced.

    Original tesxt file
    item1
    item2
    item3

    Saved text file
    string item1

    string item2

    string item3

    I think what his happening is when the items are read in lots of white
    space is attached to the end of the string.

    Is there a way to remove only trailing white space, from a string, but
    retain white spaces with in the string? thanks,
    Hi Marc,

    Unless you're doing something peculiar, nothing will be added to the
    string as you read it in. However, it will contain the EOL character -
    on DOS it's CRLF, on unix it's LF and on mac's it CR.

    If you're then adding the EOL when you're printing it, then that's your
    problem. Either chomp the input or don't add a new one on output.

    If this doesn't help, try posting the code in question so we can have a
    look.

    HTH

    Gary
    Marc

    Marc Fromm
    Computer Systems Specialist
    University of Montana--Western
    710 South Atlantic Street
    Dillon, MT 59725
    406-683-7164



    ________________________________________________________________
    Sent via the HotDawg Mail system at hotdawg.umwestern.edu
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

    ---
    [This E-mail scanned for viruses by Declude Virus]
    ________________________________________________________________
    Sent via the HotDawg Mail system at hotdawg.umwestern.edu
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 7, '03 at 4:58a
activeFeb 10, '03 at 9:24a
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase