加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 是否有一个单行来获得分割的第一个元素?

发布时间:2020-12-15 21:17:59 所属栏目:大数据 来源:网络整理
导读:而不是写: @holder = split /./,"hello.world"; print @holder[0]; 是否有可能只做一个单行,只是得到分裂的第一个元素?就像是: print (split /./,"hello.world")[0] 当我尝试第二个例子时,我收到以下错误: print (...) interpreted as function at t
而不是写:
@holder = split /./,"hello.world"; 
print @holder[0];

是否有可能只做一个单行,只是得到分裂的第一个元素?就像是:

print (split /./,"hello.world")[0]

当我尝试第二个例子时,我收到以下错误:

print (...) interpreted as function at test.pl line 3.
syntax error at test.pl line 3,near ")["

解决方法

你应该尝试你的预感。这是怎么做到的。
my $first = (split /./,"hello.world")[0];

您可以使用仅抓取第一个字段的列表上下文分配。

my($first) = split /./,"hello.world";

要打印,请使用

print +(split /./,"hello.world")[0],"n";

要么

print ((split(/./,"hello.world"))[0],"n");

加号是因为句法歧义。它表示以下所有内容都是要打印的参数。 perlfunc documentation on print解释。

Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a +,but that doesn’t look as good).

在上述情况下,我发现这种情况更容易编写和阅读。因人而异。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读