#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2

    Successfully removed 0d0a, but have a question


    Hello,
    I had a file which had 0d0a at the end and was giving me some problems, but I have managed to get rid of it using the following code.

    My only question however is, how do I change s/\r\n\z//; when I'm using the while loop as while ($line = <FILE>). I understand that I will have to perform s/\r\n\z//; on the $line, but I'm not able to.

    Code:
    #!/usr/local/bin/perl
    
    open (FILE,'myfile.txt') or die("could not open file myfile.txt");
    
    while(<FILE>)
    {
     s/\r\n\z//;
     chomp($_);
     print "line == [$_]\n";
     $filenamearray[$i++] = $_;
    }
    
    $countelements = @filenamearray;
    for($i = 0;$i<$countelements ; $i++)
    {
     print "element$i == [$filenamearray[$i]]\n";
    }
    (In case my question is not clear, taking the example of chomp, I'm asking what's the equivalent of chomp($line) when using while ($line = <FILE>) as against chomp; when using while(<FILE>))


    Thanks.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,261
    Rep Power
    1810
    Code:
    while (<FILE>) {
        s/\r\n\z//;
        chomp($_);
    }
    $_ is the default variable. No need to specify it even in the chomp:

    Code:
    while (<FILE>) {
        chomp;
        s/\r\n\z//;
    }
    Also note that you can change the input record separator, contained in $/. That's the variable perl is using when reading the file, and which by default is "\n" on Linux, and "\r\n" on Windows. If you are reading a Windows file on a *nix machine, that's a good time to make the adjustment:

    Code:
    {
        local $/ = "\r\n";
    
        while (<FILE>) {
          chomp;
          print;
        }
    }
    The while loop will separate the lines at whatever record separator you want to use, and then chomp removes the record separator ("\r\n" in this case) from the end of $_.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by keath
    Code:
    while (<FILE>) {
        chomp;
        s/\r\n\z//;
    }
    Thanks keath, but what if my code was more like
    while ($line = <FILE>) #assume it is a requirement to save the current line in $line
    {
    chomp($line);
    #s/\r\n\z//; #but how do I apply this to $line ?
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Try this:

    Perl Code:
    while ($line = <FILE>) {
         chomp $line;
         $line =~ s/[\r\n\z]//g; 
    }


    BTW, the chomp is now useless with the next line doing again the same (and more).

    Also not sure that you really need the \z in the regex.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by Laurent_R
    Try this:

    Perl Code:
    while ($line = <FILE>) {
         chomp $line;
         $line =~ s/[\r\n\z]//g; 
    }


    BTW, the chomp is now useless with the next line doing again the same (and more).

    Also not sure that you really need the \z in the regex.
    Thanks a lot Laurent_R, worked fine
  10. #6
  11. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,261
    Rep Power
    1810
    Originally Posted by IAMTubby
    Thanks keath, but what if my code was more like
    while ($line = <FILE>) #assume it is a requirement to save the current line in $line
    {
    chomp($line);
    #s/\r\n\z//; #but how do I apply this to $line ?
    }
    Sorry about that. I fully intended to answer that question, but then I fell asleep.

    To apply a regex to a variable, use the =~ operator.

    Code:
    while (my $line = <FILE>) {
        $line =~ s/\r\n\z//;
    }
    Edit: still not awake; question already answered.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by keath
    Sorry about that. I fully intended to answer that question, but then I fell asleep.
    Haha thanks a lot keath, no problems

IMN logo majestic logo threadwatch logo seochat tools logo