BadMojo has asked for the wisdom of the Perl Monks concerning the following question:

hello every one I think this should be a simple one can any one tell me how to test for a new line? I have an array that takes the data from a file IE (@myvar=<FILEPTR>;) my file should only have one line but some users may add a empty new line I am looking for a way to test for this my idea as of now is to loop through the array after the first line is evaluated to be larger then 0, so the first line does have at lest one character on it. then to test all other lines to be sure they only have new lines "\n" is it as easy as this $mfvar[x] eq "\n"? I know this wouldn't work in c.

BadMojo
mford@badmojo.biz
www.badmojo.biz

!=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-!
When something works but shouldn't thats BADMOJO baby.

Replies are listed 'Best First'.
Re: How to test for a new line
by tphyahoo (Vicar) on May 13, 2005 at 15:04 UTC
    Perl is kind to people reading in lines from a file. Usually you do this with
    (while <FILE>) { chomp; print "$_\n"; # prints the current line. }
    In your code, @myvar should also already be an array of lines, unless you set the input record separator $/ to undef (which you almost certainly didn't so don't worry about it). Chomp gets rid of the new line at the end of the line under usual circumstances. Maybe that helps, maybe not. What exactly are you trying to do?
Re: How to test for a new line
by jeffa (Bishop) on May 13, 2005 at 15:09 UTC

    If you only need the first line ... then only grab the first line:

    my $line = <FILEPTR>;
    But like dragonchild requested, we can't really help you completely unless we know more.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      I need to know if its truly a new line or if they spanned multiple lines so i can give a understanable error to the user. I will try eq again

      BadMojo
      mford@badmojo.biz
      www.badmojo.biz

      !=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-!
      When something works but shouldn't thats BADMOJO baby.

        You should have specified that in your original question. "... to be sure they only have new lines" is not the same as "give a understanable error to the user." Now then, here's how i might check for such an "error":

        my @array = <FILEHANDLE>; if (@array < 1) { # we have no lines, send an error message } eslif (@array > 1) { # we have a 'too many lines' error jim, send a message } else { # this porridge tastes *just* right ... continue with processing }
        When you slurp a filehandle into an array, you can always count the number of lines by counting how many elements the array has. No need to explicitly check for newlines.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
        here's a thought... could you join all of the lines together (maybe chomp them all first), then you get one big line. then, you don't have to worry if the user's input spans into the second line or if they have new lines. this is assuming that a file contains only one 'line' of useful information though.
        my $line = join '', map { chomp } <FILEPTR>;
Re: How to test for a new line
by tlm (Prior) on May 13, 2005 at 19:56 UTC

    I'd do something like (untested)

    my $myvar; { local $/ = undef; if ( ( $myvar = <FILEPTR> ) =~ s/\n+//g ) { alert_user(); } }
    If you don't care to alert the user when newlines are found, then replace the if block with
    ( $myvar = <FILEPTR> ) =~ s/\n+//g;
    The line
    local $/ = undef;
    essentially turns off line-oriented input, and reading from the handle gets the whole string.

    If you want to preserve the last newline (and just get rid of the embedded ones), use this regular expression instead:

    s/\n+(?!\z)//g

    the lowliest monk

Re: How to test for a new line
by dragonchild (Archbishop) on May 13, 2005 at 15:01 UTC
    Did you try it? What happened? Was it helpful?

    In other words, it would be very simple to set up a few test cases (you should have them anyways!) and see if it works as you hope.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
      aaahhhh for any one else who may want to know how to do this I got it to work by doing this

      $myvar1 == "\n"

      BadMojo
      mford@badmojo.biz
      www.badmojo.biz

      !=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-!
      When something works but shouldn't thats BADMOJO baby.

        Try 'eq' and you'll be better off.

        Oh - turn on warnings. You'll have found you're not doing what you think you're doing.


        • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
        • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

        That's only working because whatever's in $myvar1 and "\n" both happen to evaluate to 0 in a numeric context. You really meant eq; see perldoc perlop.

        Update: Now if what you really mean is "is it a blank line with nothing but whitespace" you probably want $myvar1 =~ /^\s*$/