string 在 Perl 中将字符串拆分为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16872340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a string into array in Perl
提问by Rahul Reddy
my $line = "file1.gz file2.gz file3.gz";
my @abc = split('', $line);
print "@abc\n";
Expected output:
预期输出:
file1.gz
file2.gz
file3.gz
I want the output to be file1.gz
in $abc[0]
, file2.gz
in $abc[1]
, and file3.gz
in $abc[2]
. How do I split $line
?
我希望输出为file1.gz
in $abc[0]
、file2.gz
in$abc[1]
和 file3.gz
in $abc[2]
。我如何拆分$line
?
回答by raina77ow
Splitting a string by whitespace is very simple:
按空格拆分字符串非常简单:
print $_, "\n" for split ' ', 'file1.gz file1.gz file3.gz';
This is a special form of split
actually (as this function usually takes patterns instead of strings):
这是split
实际上的一种特殊形式(因为此函数通常采用模式而不是字符串):
As another special case,
split
emulates the default behavior of the command line toolawk
when thePATTERN
is either omitted or a literal string composed of a single space character (such as' '
or"\x20"
). In this case, any leading whitespace inEXPR
is removed before splitting occurs, and thePATTERN
is instead treated as if it were/\s+/
; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator.
作为另一种特殊情况,当被省略或由单个空格字符(例如或)组成的文字字符串时,
split
模拟命令行工具的默认行为。在这种情况下,任何前导空格 in在拆分发生之前被删除,而是被视为; 特别是,这意味着任何连续的空格(不仅仅是单个空格字符)都用作分隔符。awk
PATTERN
' '
"\x20"
EXPR
PATTERN
/\s+/
Here's an answer for the original question (with a simple string without any whitespace):
这是原始问题的答案(带有一个没有任何空格的简单字符串):
Perhaps you want to split on .gz
extension:
也许您想拆分.gz
扩展名:
my $line = "file1.gzfile1.gzfile3.gz";
my @abc = split /(?<=\.gz)/, $line;
print $_, "\n" for @abc;
Here I used (?<=...)
construct, which is look-behind assertion, basically making split at each point in the line preceded by .gz
substring.
在这里,我使用了后视断言的(?<=...)
构造,基本上在子字符串前面的行中的每个点进行拆分。.gz
If you work with the fixed set of extensions, you can extend the pattern to include them all:
如果您使用固定的扩展集,您可以扩展模式以包含它们:
my $line = "file1.gzfile2.txtfile2.gzfile3.xls";
my @exts = ('txt', 'xls', 'gz');
my $patt = join '|', map { '(?<=\.' . $_ . ')' } @exts;
my @abc = split /$patt/, $line;
print $_, "\n" for @abc;
回答by e2-e4
Having $line
as it is now, you can simply split the string based on at least one whitespace separator
有$line
像现在,你可以基于至少一个空格分隔简单地分割字符串
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
.
我更喜欢使用()
for split
,print
和for
。
回答by void
I found this one to be very simple!
我发现这个非常简单!
my $line = "file1.gz file2.gz file3.gz";
my @abc = ($line =~ /(\w+[.]\w+)/g);
print $abc[0],"\n";
print $abc[1],"\n";
print $abc[2],"\n";
output:
输出:
file1.gz
file2.gz
file3.gz
Here take a look at this tutorial to find more on Perl regular expressionand scroll down to More matchingsection.
在这里查看本教程以查找有关Perl 正则表达式的更多信息并向下滚动到更多匹配部分。
回答by Thanos
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.
要在 Perl 中查看数据结构,您可以使用Data::Dumper
. 要打印字符串,您可以使用say
,它"\n"
在每次调用后添加一个换行符,而不是显式添加它。
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
.
我通常使用\s
which 匹配空白字符。如果添加+
它匹配一个或多个空白字符。您可以在此处阅读更多相关信息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;
回答by user3682640
Just use /\s+/ against '' as a splitter. In this case all "extra" blanks were removed. Usually this particular behaviour is required. So, in you case it will be:
只需使用 /\s+/ 对 '' 作为分隔符。在这种情况下,所有“额外”的空白都被删除了。通常需要这种特殊行为。所以,在你的情况下,它将是:
my $line = "file1.gz file1.gz file3.gz";
my @abc = split(/\s+/, $line);