in reply to Re^2: While loop conditions in Perl
in thread While loop conditions in Perl

Well I actually don't use chomp anymore, I can't really explain it but I have come across files that just didn't agree with chomp. So I use...

$string =~ s/^\s+//; $string =~ s/\s+$//;

To remove any whitespace(including newlines) before the first character and after the last character on the line, as well as...

@fi = split(/\s+/, $nextline);

... to break apart the line by spaces.

This whole issue I was having actually initiated with my while loop being constructed with...

while(@fi[0] eq'') { $nextline = readline<IN>; //remove whitespace //split the line, but I think I included my accidentally so that +I had my @fi = split..... }

...so that I also changed my scope inside the while loop accidentally by using...

 my $nextline = ...

instead of...

 $nextline = ...

Replies are listed 'Best First'.
Re^4: While loop conditions in Perl
by cheekuperl (Monk) on Jul 19, 2012 at 06:00 UTC
    Thats alright my friend. You could've said it all in one line :)
    I can't really explain it but I have come across files that just didn't agree with chomp
    Now this has made me curious! Could you please elaborate?
    (This might help me if I come across any such situation and I have no clues about what's going on)

      Perhaps a file from another OS with line endings that do not match the local machine's expectations?

      Well it won't make sense and I still don't understand it but I came across files that behaved as though they had multiple \n hidden on the same line. If that is even possible? I figured it out when I read a line, chomped it, but still had three blank lines between in and the next line with text. However, if I were to chomp the string(line) again, I only had two blank lines between the first line with text and the next line with text. I wish I remembered what it was but I could have sworn I used a text editor some time in the past that allowed you to see \c \t \n etc etc. Would have been helpful.

        Okay. So is it that we are not sure that the mysterious '\n' were really '\n'? If you didn't view them in text editors, there's every chance they were '\n' in fact. Anyway, do update when you come across such a situation again :)
Re^4: While loop conditions in Perl
by Anonymous Monk on Oct 18, 2013 at 21:31 UTC
    I believe you have probably encountered line feeds followed by new lines ( \r\n ). In this case, just adjust the input record separator ( local $/ = "\r\n"; )