开发者

Having problem splitting a sting around the newline character

开发者 https://www.devze.com 2023-02-03 07:06 出处:网络
Here is another one of these weird things. I have this code and a file. use strict; use warnings; my $file = \"test.txt\";

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消