简体   繁体   English

使用 Perl 按点分割

[英]Split by dot using Perl

I use the split function by two ways.我通过两种方式使用split功能。 First way (string argument to split ):第一种方式(字符串参数split ):

my $string = "chr1.txt";
my @array1 = split(".", $string);
print $array1[0];

I get this error:我收到此错误:

Use of uninitialized value in print在打印中使用未初始化的值

When I do split by the second way (regular expression argument to split ), I don't get any errors.当我通过第二种方式(正则表达式参数)做分裂,我没有得到任何错误。

my @array1 = split(/\./, $string); print $array1[0];

My first way of splitting is not working only for dot.我的第一种拆分方式不仅适用于点。

What is the reason behind this?这背后的原因是什么?

"\\." is just .只是. , careful with escape sequences. ,小心转义序列。

If you want a backslash and a dot in a double-quoted string, you need "\\\\."如果要在双引号字符串中使用反斜杠和点,则需要"\\\\." . . Or use single quotes: '\\.'或者使用单引号: '\\.'

如果您只想解析文件并获取它们的后缀,最好使用File::Basenamefileparse()方法。

Additional details to the information provided by Mat : Mat 提供的信息的其他详细信息

In split "\\.", ... the first parameter to split is first interpreted as a double-quoted string before being passed to the regex engine.split "\\.", ... split的第一个参数在传递给正则表达式引擎之前首先被解释为双引号字符串。 As Mat said, inside a double-quoted string, a \\ is the escape character, meaning "take the next character literally", eg for things like putting double quotes inside a double-quoted string: "\\""正如 Mat 所说,在双引号字符串中, \\是转义字符,意思是“按字面意思取下一个字符”,例如将双引号放在双引号字符串中: "\\""

So your split gets passed "."所以你的split被传递了"." as the pattern.作为图案。 A single dot means "split on any character".单个点表示“在任何字符上拆分”。 As you know, the split pattern itself is not part of the results.如您所知,拆分模式本身不是结果的一部分。 So you have several empty strings as the result.所以你有几个空字符串作为结果。

But why is the first element undefined instead of empty?但是为什么第一个元素未定义而不是空? The answer lies in the documentation for split : if you don't impose a limit on the number of elements returned by split (its third argument) then it will silently remove empty results from the end of the list.答案在于split的文档中:如果您不对split (它的第三个参数)返回的元素数量施加限制,那么它将悄悄地从列表的末尾删除空结果。 As all items are empty the list is empty, hence the first element doesn't exist and is undefined.由于所有项目都是空的,列表是空的,因此第一个元素不存在且未定义。

You can see the difference with this particular snippet:您可以看到此特定代码段的不同之处:

 my @p1 = split "\.", "thing";
 my @p2 = split "\.", "thing", -1;
 print scalar(@p1), ' ', scalar(@p2), "\n";

It outputs 0 6 .它输出0 6

The "proper" way to deal with this, however, is what @soulSurfer2010 said in his post.然而,处理这个问题的“正确”方法是@soulSurfer2010 在他的帖子中所说的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM