简体   繁体   English

perl分裂奇怪的行为

[英]perl split strange behavior

I apologize in advance, this is probably a very stupid question with an obvious solution which is escaping the eye of a rather beginner in perl, or it may also have been in Stackoverflow as a solved question, but my lack of knowledge about what exactly to look for is preventing me from actually finding the answer. 我提前道歉,这可能是一个非常愚蠢的问题,有一个显而易见的解决方案,它逃避了一个相当初学者的眼睛,或者它也可能已经在Stackoverflow中作为一个解决的问题,但我缺乏关于究竟是什么的知识寻找是阻止我实际找到答案。

I have a string like: 我有一个字符串:

$s = FOO: < single blankspace> BAR <some whitespace character> some more text with     whitespace that can span over multiple lines, i.e. has \n in them ; 

#please excuse the lack of quotes, and large text describing the character in angular brackets, but in this example, but I have the string correctly defined, and in plase of <blankspace> I have the actual ASCII 32 character etc.

Now I want to split the $s, in this way: 现在我想以这种方式拆分$ s:

($instType, $inst, $trailing) = split(/\s*/, $s, 3);
#please note that i do not use the my keyword as it is not in a subroutine
#but i tested with my, it does not change the behavior

I would expect, that $instType takes the value FOO: , without any surrounding space, in the actual test string there is a colon, and I believe, to the best of my knowledge, that it will remain in the $instType. 我希望,$ instType取值FOO :,没有任何周围的空间,在实际的测试字符串中有一个冒号,我相信,据我所知,它将保留在$ instType中。 Then it is rather obvious to expect that $inst takes similary the value BAR , without any surrounding spaces, and then finally one may also lean on $trail to take the rest of the string. 然后很明显地期望$ inst与BAR相似,没有任何周围的空间,然后最后一个人也可以依靠$ trail来获取其余的字符串。

However, I am getting: $instType takes F , that is just the single char, $inst takes O, the single charater in the 2nd position in the string $trail takes O: BAR and the rest. 但是,我得到:$ instType需要F,这只是单个字符,$ inst需要O,字符串$ trail中第二个位置的单个字符需要O:BAR和其余部分。

How do I address the issue? 我该如何解决这个问题?

PS perl is 5.18.0 PS perl是5.18.0

the problem is the quantifier * that allows zero space (zero or more), you must use + instead, that means 1 or more. 问题是允许零空间(零或更多)的量词* ,你必须使用+代替,这意味着1或更多。

Note that there is exactly zero space between F and O. 请注意,F和O之间的空间正好为零。

You wrote: 你写了:

#please note that i do not use the my keyword as it is not in a subroutine
#but i tested with my, it does not change the behavior

You can, and should, use my outside of subroutines, too. 你也可以而且应该使用my的子程序之外的东西。 Using that in conjunction with use strict prevents silly errors like this: 结合use strict使用它可以防止像这样的愚蠢错误:

$some_field = 'bar';
if ( $some_feild ) { ... }

If those statements were separated, it could be awfully hard to track down that bug. 如果这些陈述是分开的,那么追查那个bug可能非常困难。

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

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