Split a string into array in Perl

后端 未结 5 1837
清歌不尽
清歌不尽 2021-02-06 00:24
my $line = \"file1.gz file2.gz file3.gz\";
my @abc = split(\'\', $line);
print \"@abc\\n\";

Expected output:

file1.gz
file2.gz
file3.gz         


        
5条回答
  •  走了就别回头了
    2021-02-06 01:00

    Having $line as it is now, you can simply split the string based on at least one whitespace separator

    my @answer = split(' ', $line); # creates an @answer array
    

    then

    print("@answer\n");               # print array on one line
    

    or

    print("$_\n") for (@answer);      # print each element on one line
    

    I prefer using () for split, print and for.

提交回复
热议问题