parse newline

Discussion in 'Perl Misc' started by ela, Feb 8, 2009.

  1. ela

    ela Guest

    It's always a nightmare for me to parse newline characters. No matter \n,
    10, 13, I just don't know why some newlines are printed. As the file is
    generated by another program, I cannot see the source code, and manual
    inspection does not discover any abnormalty.

    using chop, chomp or to use replace cannot help. Does anybody have
    experience in handling this?
    ela, Feb 8, 2009
    #1
    1. Advertising

  2. On Sun, 08 Feb 2009 17:05:47 +0800, ela wrote:

    > It's always a nightmare for me to parse newline characters. No matter
    > \n, 10, 13, I just don't know why some newlines are printed. As the file
    > is generated by another program, I cannot see the source code, and
    > manual inspection does not discover any abnormalty.
    >
    > using chop, chomp or to use replace cannot help. Does anybody have
    > experience in handling this?


    Something like:

    chomp;
    s/\x0d$//;

    Should always do the trick. If not, you have something very strange going
    on.

    M4
    Martijn Lievaart, Feb 8, 2009
    #2
    1. Advertising

  3. "ela" <> wrote:
    >It's always a nightmare for me to parse newline characters. No matter \n,
    >10, 13, I just don't know why some newlines are printed. As the file is
    >generated by another program, I cannot see the source code, and manual
    >inspection does not discover any abnormalty.
    >
    >using chop, chomp or to use replace cannot help. Does anybody have
    >experience in handling this?


    Your program is missing a semicolon on line 42.

    jue
    Jürgen Exner, Feb 8, 2009
    #3
  4. ela

    ela Guest

    >
    > Something like:
    >
    > chomp;
    > s/\x0d$//;
    >
    > Should always do the trick. If not, you have something very strange going
    > on.
    >


    Have already tried that before and it doesn't solve...
    ela, Feb 8, 2009
    #4
  5. ela

    ela Guest

    "Jürgen Exner" <> wrote in message
    news:...
    > "ela" <> wrote:
    >>It's always a nightmare for me to parse newline characters. No matter \n,
    >>10, 13, I just don't know why some newlines are printed. As the file is
    >>generated by another program, I cannot see the source code, and manual
    >>inspection does not discover any abnormalty.
    >>
    >>using chop, chomp or to use replace cannot help. Does anybody have
    >>experience in handling this?

    >
    > Your program is missing a semicolon on line 42.
    >
    > jue


    line 42????????

    Well I've tried :

    chop $identiy;
    chomp ($identiy);
    chop $identiy;
    $identity =~ s/\x0d$//;

    All fail.
    ela, Feb 8, 2009
    #5
  6. "ela" <> wrote:
    >"Jürgen Exner" <> wrote in message
    >> "ela" <> wrote:
    >>>It's always a nightmare for me to parse newline characters.


    What character set are you using? None of the common ASCII-based
    character sets (WIndows-1252, ISO-Latin-xxx, Unicode, ...) has a newline
    character. See also below.

    >>>No matter \n,
    >>>10, 13, I just don't know why some newlines are printed. As the file is
    >>>generated by another program, I cannot see the source code, and manual
    >>>inspection does not discover any abnormalty.

    [...]
    >> Your program is missing a semicolon on line 42.

    >
    >line 42????????


    Apparently you've never read The Hitchhikers Guide through the Galaxy.

    Long form: how do you propose us to fix your code without seeing it?
    Have you seen the posting guidelines that are posted here twice a week?

    >Well I've tried :
    >
    > chop $identiy;


    This will remove the last character of $identiy.
    How do you know that last character is actually the newline?

    > chomp ($identiy);


    This will remove a trailing $/ from $identiy, whatever $/ may be set to
    on your system (usually "\n").
    Did you check that $/ matches the tail of $identiy?

    > chop $identiy;


    This looks identical to the first line?

    > $identity =~ s/\x0d$//;


    This is working on $identity instead of $identiy. Is that what you meant
    to do?

    One common problem are format incompatibilities between Windows, Mac,
    and Unix. They use different characters/character combinations to denote
    a line break. Therefore you should be very explicit about if you are
    talking about a line feed character(LF), a carriage return
    character(CR), or a logical newline entity of your OS.

    Aside of that I suspect that you are looking at the wrong spot and your
    real problem is somewhere else, like e.g. a misspelled variable name as
    above

    As strongly suggested in the posting guidelines please post a
    self-contained, minimal program that demonstrates your problem, in your
    case including some sample input data, preferable as a _DATA_ section.

    jue
    Jürgen Exner, Feb 8, 2009
    #6
  7. "ela" <> wrote in
    news:gmm79b$dsa$:

    > It's always a nightmare for me to parse newline characters. No matter
    > \n, 10, 13, I just don't know why some newlines are printed. As the
    > file is generated by another program, I cannot see the source code,
    > and manual inspection does not discover any abnormalty.
    >
    > using chop, chomp or to use replace cannot help. Does anybody have
    > experience in handling this?
    >
    >


    You might be encountering NUL characters. I run into the NUL character
    problem when working with Windows event logs. Try to use the following on
    your data: =~ s/\0/\t/g; You can change "\t" to whatever you need.

    Example:
    # Get event data
    my $streaingTest = $Event{Strings};

    # Change NUL to tab for event data
    $streaingTest =~ s/\0/\t/g;

    ....Cos
    Cosmic Cruizer, Feb 8, 2009
    #7
  8. On Sun, 08 Feb 2009 23:36:39 +0800, ela wrote:


    >> Something like:
    >>
    >> chomp;
    >> s/\x0d$//;
    >>
    >> Should always do the trick. If not, you have something very strange
    >> going on.
    >>
    >>

    > Have already tried that before and it doesn't solve...


    Funny, did you try hexdumping the files?

    M4
    Martijn Lievaart, Feb 9, 2009
    #8
  9. On Sun, 08 Feb 2009 23:36:39 +0800, ela wrote:


    >> Something like:
    >>
    >> chomp;
    >> s/\x0d$//;
    >>
    >> Should always do the trick. If not, you have something very strange
    >> going on.
    >>
    >>

    > Have already tried that before and it doesn't solve...


    BTW, I did mean both statements and in that order.

    M4
    Martijn Lievaart, Feb 9, 2009
    #9
  10. ela

    Ted Zlatanov Guest

    On Sun, 8 Feb 2009 23:51:54 +0800 "ela" <> wrote:

    e> Well I've tried :

    e> chop $identiy;
    e> chomp ($identiy);
    e> chop $identiy;
    e> $identity =~ s/\x0d$//;

    e> All fail.

    Assuming your text is in FILE.TXT:

    od -t u1 -t a FILE.txt

    What does your text show for the lines that are not correctly processed
    by your program?

    Ted
    Ted Zlatanov, Feb 9, 2009
    #10
  11. Ted Zlatanov <> wrote:

    > Assuming your text is in FILE.TXT:

    ^^^
    ^^^
    > od -t u1 -t a FILE.txt



    s/txt/TXT/;


    :)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 10, 2009
    #11
    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. Antonio
    Replies:
    0
    Views:
    414
    Antonio
    Jan 19, 2005
  2. Gerard Oberle

    expression eats newline

    Gerard Oberle, Jul 31, 2003, in forum: Perl
    Replies:
    3
    Views:
    1,183
    Purl Gurl
    Aug 1, 2003
  3. Replies:
    19
    Views:
    1,106
    Daniel Vallstrom
    Mar 15, 2005
  4. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    175
    7stud --
    Feb 20, 2008
  5. Replies:
    10
    Views:
    229
    Uri Guttman
    Apr 30, 2010
Loading...

Share This Page