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? | [reply] [d/l] |
Re: How to test for a new line
by jeffa (Bishop) on May 13, 2005 at 15:09 UTC
|
my $line = <FILEPTR>;
But like dragonchild requested, we can't really help you completely unless we know
more.
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
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>;
| [reply] [d/l] |
Re: How to test for a new line
by tlm (Prior) on May 13, 2005 at 19:56 UTC
|
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
| [reply] [d/l] [select] |
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?"
| [reply] |
|
|
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.
| [reply] |
|
|
| [reply] |
|
|
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*$/
| [reply] [d/l] [select] |
|
|