perl,如何使用数组而不先将其分配给变量?
这是我目前在程序中所做的,
@array = split /\n/, $longstring;
$data = $array[14];
我真的只想从数组中获取位置 14 处的元素并使用它,字符串中的其他内容对我来说没有用。我知道在像java这样的语言中我可以做这样的事情,
$data = (split /\n/, $longstring)[14];
这正是我想做的,但是在perl中。
那么如何访问数组元素而不必先将数组分配给变量呢?
怎么样,
编辑:嗯,好的,那么长的方式
my $data = "abc\nd^e^f\nghi";
my @a = split (/\^/, (split /\n/, $data)[1]);
print $a[2];
__OUTPUT__
f
短的方式
my $data = "abc\nd^e^f\nghi";
my $a = split (/\^/, (split /\n/, $data)[1])[2]; # line 60
print $a;
__OUTPUT__
syntax error at script.pl line 60, near ")["
Execution of script.pl aborted due to compilation errors.
这让我比平常更困惑,因为它适用于内部分割,但不适用于外部分割
编辑2:
我有点困惑为什么这两行不同
my $a = (split /\^/, (split /\n/, $data)[1])[2]; # works
my $a = split (/\^/, (split /\n/, $data)[1])[2]; # doesnt
这是我对第二行的思考过程,这是我最初写的(换句话说,这就是我认为我的程序正在做的事情)
my $data = "abc\nd^e^f\nghi";
my $a = split (/\^/, (split /\n/, $data)[1])[2];
my $a = split (/\^/, ("abc", "d^e^f", "ghi")[1])[2];
my $a = split (/\^/, "d^e^f")[2];
my $a = ("d", "e", "f")[2];
my $a = "f";
这就是我期望发生的事情,有人能指出我的想法出了问题吗?
Here's what I currently do in my program
@array = split /\n/, $longstring;
$data = $array[14];
I really only want to get the element at position 14 out of the array and use that, the other stuff in the string is not useful to me. I know that in a language like java I could do something like this
$data = (split /\n/, $longstring)[14];
which is what I want to do but in perl.
So how do I access array elements without having to assign the array to a variable first?
edit: hmm, ok what about this
the long way
my $data = "abc\nd^e^f\nghi";
my @a = split (/\^/, (split /\n/, $data)[1]);
print $a[2];
__OUTPUT__
f
the short way
my $data = "abc\nd^e^f\nghi";
my $a = split (/\^/, (split /\n/, $data)[1])[2]; # line 60
print $a;
__OUTPUT__
syntax error at script.pl line 60, near ")["
Execution of script.pl aborted due to compilation errors.
this confuses me more than usual since it works on the inner split, but not the outer split
edit 2:
I'm a little confused as to why these two lines are different
my $a = (split /\^/, (split /\n/, $data)[1])[2]; # works
my $a = split (/\^/, (split /\n/, $data)[1])[2]; # doesnt
here's my thought process for the 2nd line which is what I wrote originally (in other words, this is what I think my program is doing)
my $data = "abc\nd^e^f\nghi";
my $a = split (/\^/, (split /\n/, $data)[1])[2];
my $a = split (/\^/, ("abc", "d^e^f", "ghi")[1])[2];
my $a = split (/\^/, "d^e^f")[2];
my $a = ("d", "e", "f")[2];
my $a = "f";
That's what I expect to happen, can someone point out where my thinking has gone awry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您刚刚放错了第一个
(
,请尝试以下操作:You've just misplaced the first
(
, try this:你写的很好。
这是键盘上的演示。
What you've written is fine.
Here's a demo at codepad.
我只是要解释为什么这些行不同:
在 Perl 中,您可以在括号中的列表上使用像
[2]
这样的数组下标(称为 列表切片)。但还有另一条规则说“如果它看起来像函数调用,那么它就是函数调用”。也就是说,当您有一个函数名称(例如split
),后跟可选的空格和左括号时,它就是一个函数调用。您不能为函数调用添加下标;您需要在其周围添加一组额外的括号。这就是我的第三行有效的原因。另一方面,您永远不应该说
my $a
或my $b
。$a
和$b
是与sort
一起使用的特殊包变量,如果您已将它们转换为词法,您将获得奇怪的问题。即使您目前没有使用sort
,您也可以稍后添加它。最简单的方法是完全避免使用$a
或$b
词法。为了便于阅读,我可能会稍微调整一下空格并添加注释:
I'm just going to explain why these lines are different:
In Perl, you can use an array subscript like
[2]
on a list in parentheses (it's called a list slice). But there's another rule that says "If it looks like a function call, it's a function call." That is, when you have the name of a function (likesplit
) followed by optional whitespace and an open parenthesis, it's a function call. You can't subscript a function call; you'd need to add an extra set of parentheses around it. That's why my third line works.On another note, you should never say
my $a
ormy $b
.$a
and$b
are special package variables that are used withsort
, and if you've turned them into lexicals, you'll have weird problems. Even if you're not usingsort
at the moment, you might add it later. It's easiest to just completely avoid ever making$a
or$b
lexical.For readability, I might adjust the whitespace a bit and add a comment: