Why failing correcting new line at end of text file

Discussion in 'Perl Misc' started by O_TEXT, Jul 29, 2009.

  1. O_TEXT

    O_TEXT Guest

    I did two functions to correct new line at end of text file. One works
    the other does not.

    A text file generally ends by one new line, but sometimes does not, for
    whatever reason. This leaded perl to have both \z and \Z.

    The aim of my function is to transform a buffer, appending a new line at
    the end of buffer, if missing.

    Function/routine test1 does it successfully.
    Function/routine test2 appends an excessive new line when new line is
    yet present as illustrated by example here after.

    Even if I will try to do the job an other way, I wonder why test2 fails?


    sub printString ($)
    {
    my ($v)=@_;
    my @val = unpack("U0C*", $v);
    for my $i ( @val)
    {
    printf "%x " , $i ;
    }
    print "\n" ;
    }

    sub test1($)
    {
    my ($v)=@_;
    $v =~ s/([^\x{000A}])\z/$1\n/smg ;
    return $v;
    }

    sub test2($)
    {
    my ($v)=@_;
    my $x000a = pack("U0C*", 0x0a); # U+000A <control>

    $v =~ s/((?:$x000a)?)\z/\n/smg ;
    return $v;
    }

    sub test($)
    {
    my ($v)=@_;
    printString ($v);
    my $r1 = test1($v);
    my $r2 = test2($v);
    printString ($r1);
    printString ($r2);
    print "1>" . $r1 . "<1 2>" . $r2 ."<2\n";
    }

    test (" y\ny\n" );
    test (" x\nx" );
    O_TEXT, Jul 29, 2009
    #1
    1. Advertising

  2. O_TEXT

    O_TEXT Guest

    O_TEXT a écrit :
    > I did two functions to correct new line at end of text file. One works
    > the other does not.
    >
    > A text file generally ends by one new line, but sometimes does not, for
    > whatever reason. This leaded perl to have both \z and \Z.
    >
    > The aim of my function is to transform a buffer, appending a new line at
    > the end of buffer, if missing.
    >
    > Function/routine test1 does it successfully.
    > Function/routine test2 appends an excessive new line when new line is
    > yet present as illustrated by example here after.
    >
    > Even if I will try to do the job an other way, I wonder why test2 fails?



    > $v =~ s/((?:$x000a)?)\z/\n/smg ;


    In fact, it is related to an extra g in the regular expression. test3
    corrects this.


    sub printString ($$)
    {
    my ($h, $v)=@_;
    my @val = unpack("U0C*", $v);
    printf "$h: " ;
    for my $i ( @val)
    {
    printf "%x " , $i ;
    }
    print "\n" ;
    }

    sub test1($)
    {
    my ($v)=@_;
    $v =~ s/([^\x{000A}])\z/$1\n/smg ;
    return $v;
    }

    sub test2($)
    {
    my ($v)=@_;
    my $x000a = pack("U0C*", 0x0a); # U+000A <control>

    $v =~ s/((?:$x000a)?)\z/\n/smg ;
    return $v;
    }

    sub test3($)
    {
    my ($v)=@_;
    my $x000a = pack("U0C*", 0x0a); # U+000A <control>

    $v =~ s/((?:$x000a)?)\z/\n/sm ;
    return $v;
    }


    sub test($)
    {
    my ($v)=@_;
    printString ('input', $v);
    my $r1 = test1($v);
    my $r2 = test2($v);
    my $r3 = test3($v);
    printString ('test1', $r1);
    printString ('test2', $r2);
    printString ('test3', $r3);
    }

    test (" y\ny\n" );
    test (" x\nx" );
    O_TEXT, Jul 29, 2009
    #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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  2. walterbyrd
    Replies:
    7
    Views:
    300
    Asun Friere
    May 17, 2007
  3. Andreas Leitgeb
    Replies:
    0
    Views:
    435
    Andreas Leitgeb
    May 15, 2009
  4. Mark Space
    Replies:
    0
    Views:
    470
    Mark Space
    May 15, 2009
  5. Sue
    Replies:
    7
    Views:
    148
    Richard Cornford
    Dec 10, 2003
Loading...

Share This Page