Converting multi-line string to array

前端 未结 5 1299
醉话见心
醉话见心 2020-12-07 00:17

How do I convert a multiline string to array?

my $text= \" ads da
sda
s 
da
d
as

das
d a as dasd
\\n

\";

Note : I dont want to re

5条回答
  •  盖世英雄少女心
    2020-12-07 01:00

    My sense is you are focusing on the wrong problem.

    Instead of trying to convert a scalar multi-line string constant into a list, maybe your question should be "How do I have a multi-line string initiated into a Perl list or array?"

    Look at Perl's List value constructors in Perldata.

    Of particular applicability to your question is how to use a heredoc to initiate an array with a multi-line string:

    #!/usr/bin/perl
    use strict; use warnings;
    use YAML;
    
    my @text= <

    Prints:

    ---
    - " ads da\n"
    - "sda\n"
    - "s \n"
    - "da\n"
    - "d\n"
    - "as\n"
    - "\n"
    - "das\n"
    - "d a as dasd\n"
    - "\n"
    - "\n"
    - "\n"
    

    Use the idioms Luke!

提交回复
热议问题