Perl `split` 不会`split` 到默认数组 [英] Perl `split` does not `split` to default array

查看:63
本文介绍了Perl `split` 不会`split` 到默认数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 split 有一个奇怪的问题,因为它默认不会 split 到默认数组中.

I have this strange problem with split in that it does not by default split into the default array.

下面是一些玩具代码.

#!/usr/bin/perl

$A="A:B:C:D";
split (":",$A);
print $_[0];

这不会打印任何内容.但是,如果我显式地拆分为像

This does not print anything. However if I explicitly split into the default array like

#!/usr/bin/perl

$A="A:B:C:D";
@_=split (":",$A);
print $_[0];

正确打印 A.我的 perl 版本是 v5.22.1.

It's correctly prints A. My perl version is v5.22.1.

推荐答案

split 默认不去@_.@_ 不像 $_ 那样工作.它仅用于函数的参数.perlvar 说:

split does not go to @_ by default. @_ does not work like $_. It's only for arguments to a function. perlvar says:

在子例程中,数组@_ 包含传递给该子例程的参数.在子例程中,@_ 是数组运算符 pop 和 shift 的默认数组.

Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine, @_ is the default array for the array operators pop and shift.

如果你使用 use strictuse warnings 运行你的程序,你会看到

If you run your program with use strict and use warnings you'll see

然而,如果没有提供任何内容,split 确实使用 $_ 作为它的第二个参数(被拆分的字符串).但是您必须始终使用返回值.

However, split does use $_ as its second argument (the string that is split up) if nothing is supplied. But you must always use the return value for something.

这篇关于Perl `split` 不会`split` 到默认数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆