Split a string into array in Perl

后端 未结 5 2050
清歌不尽
清歌不尽 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:20

    You already have multiple answers to your question, but I would like to add another minor one here that might help to add something.

    To view data structures in Perl you can use Data::Dumper. To print a string you can use say, which adds a newline character "\n" after every call instead of adding it explicitly.

    I usually use \s which matches a whitespace character. If you add + it matches one or more whitespace characters. You can read more about it here perlre.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    use feature 'say';
    
    my $line = "file1.gz file2.gz file3.gz";
    my @abc  = split /\s+/, $line;
    
    print Dumper \@abc;
    say for @abc;
    

提交回复
热议问题