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.gzin
$abc[2]`. How do I split?
我希望输出是file1。广州在abc[0],美元file2。广州在美国广播公司[1]美元,美元andfile3.gzin abc[2]”。我怎么分?
5 个解决方案
#1
14
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):
这是一种特殊的分割形式(因为这个函数通常采用模式而不是字符串):
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模拟命令行工具awk的默认行为,当模式被省略或由单个空格字符组成的文字字符串(例如“或”\x20)时。在这种情况下,EXPR中的任何主要空格在发生分裂之前都被删除,而模式被当作是/\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;
#2
9
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
.
我更喜欢用()分割、打印和使用。
#3
0
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);
for my $i in (@abc) {
print "$i\n";
}
#4
0
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 expression and scroll down to More matching section.
在这里,您可以看看本教程,以找到更多关于Perl正则表达式的内容,并向下滚动到更多的匹配部分。
#5
0
Hello Rahul Reddy,
你好,拉胡尔Reddy,
You already have multiple answers to your question, though I would like to add another minor one here that might help to add something.
你的问题已经有多个答案了,不过我想在这里添加一个小的答案,可能有助于添加一些东西。
To view complex Data Structures in Perl you can use Data::Dumper and also to print a string you can use say, it adds a new line character (\n
) after every string instead of doing it manually.
要查看Perl中复杂的数据结构,您可以使用数据::Dumper,也可以打印您可以使用的字符串,它在每个字符串后面添加一个新的行字符(\n),而不是手工操作。
I usually use the (\s
) that matches (one) whitespace character, if you add the (+
) it matches one or more whitespaces (\s+)
. You can read more about it here perlre.
我通常使用匹配(一个)空格字符的(\s),如果您添加(+)它匹配一个或多个空格(\s+)。你可以在这里读到更多。
Sample of code:
示例代码:
#!/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);
Hope this helps, BR.
希望这有助于,BR。
#1
14
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):
这是一种特殊的分割形式(因为这个函数通常采用模式而不是字符串):
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模拟命令行工具awk的默认行为,当模式被省略或由单个空格字符组成的文字字符串(例如“或”\x20)时。在这种情况下,EXPR中的任何主要空格在发生分裂之前都被删除,而模式被当作是/\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;
#2
9
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
.
我更喜欢用()分割、打印和使用。
#3
0
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);
for my $i in (@abc) {
print "$i\n";
}
#4
0
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 expression and scroll down to More matching section.
在这里,您可以看看本教程,以找到更多关于Perl正则表达式的内容,并向下滚动到更多的匹配部分。
#5
0
Hello Rahul Reddy,
你好,拉胡尔Reddy,
You already have multiple answers to your question, though I would like to add another minor one here that might help to add something.
你的问题已经有多个答案了,不过我想在这里添加一个小的答案,可能有助于添加一些东西。
To view complex Data Structures in Perl you can use Data::Dumper and also to print a string you can use say, it adds a new line character (\n
) after every string instead of doing it manually.
要查看Perl中复杂的数据结构,您可以使用数据::Dumper,也可以打印您可以使用的字符串,它在每个字符串后面添加一个新的行字符(\n),而不是手工操作。
I usually use the (\s
) that matches (one) whitespace character, if you add the (+
) it matches one or more whitespaces (\s+)
. You can read more about it here perlre.
我通常使用匹配(一个)空格字符的(\s),如果您添加(+)它匹配一个或多个空格(\s+)。你可以在这里读到更多。
Sample of code:
示例代码:
#!/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);
Hope this helps, BR.
希望这有助于,BR。