Having problem splitting a sting around the newline character
Here is another one of these weird things. I have this code and a file.
use strict;
use warnings;
my $file = "test.txt";
my @arr;
open (LOGFILE, $file);
while (my $line = <LOGFILE>)
{
#print $line;
@arr = split("\n", $line);
}
close LOGF开发者_运维问答ILE;
print $arr[1];
test.txt contains
\ntest1 \ntest2 \ntest3
Here is the error I get:
Use of uninitialized value in print at test.pl line 15.
Did anyone encounter a similar problem in the past?
split
takes a regex (I believe your string is coerced into a regex). Maybe something like split(/\\n/, $line)
?
use strict;
use warnings;
my $file = "test.txt";
my @arr;
open (LOGFILE, $file);
while (my $line = <LOGFILE>)
{
print $line;
@arr = split(/\\n/, $line);
}
close LOGFILE;
print $arr[1];
You could use:
@arr = split /\Q\n/, $line;
精彩评论