关于优先+重复修饰符的问题 [英] Question about precedence + repetition modifer

查看:90
本文介绍了关于优先+重复修饰符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请您向我解释这种明显不一致的行为:

Please could you explain this apparently inconsistent behaviour to me:

use strict;
my @a;

print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa

print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa

最后一个不应该打印一个'a'吗?

Shouldn't the last one print a single 'a'?

好吧,现在这对我来说更有意义: 二进制"x"是重复运算符...在列表上下文中,如果左操作数括在括号中,或者是由qw/STRING/组成的列表,它将重复该列表." perlop

Ok so now this is kind of making more sense to me: "Binary "x" is the repetition operator...In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list." perlop

这对我来说就像泥泞一样(二进制x-为什么使用二进制这个词?是否有拒绝X?) 但不管怎么说: @a =("a")x 2#似乎在列表上下文中,因为我们在开头有一个数组-数组不是列表,但是它包含一个列表,所以我认为我们可能有一个列表上下文,(虽然它们可能是同义词,但不是数组上下文).

This is as clear as mud to me (binary x - why use the word binary? Is there a denary X?) But anyway: @a = ("a") x 2 # seems to be in list context, in that we have an array at the beginning - an array is not a list, but it contains a list, so I think we probably have a list context, (not an array context although they might be synonymous).

我假设左操作数"为("a"). (要么是那个,要么就是@a). perlop没有说出操作数实际上是什么,查询perldoc.perl.org给出没有找到匹配项",而谷歌搜索给出在计算机编程中,操作数是一个用来描述能够被操纵的对象的术语".例如,像数组.

i suppose the "left operand" is ("a"). (It's either that or @a). perlop doesn't say what an operand actually is, querying perldoc.perl.org gives "No matches found", and googling gives "In computer programming, an operand is a term used to describe any object that is capable of being manipulated." Like an array for instance.

因此,左操作数可能被括在方括号中,因此它应该重复该列表".列表为:("a") x 2 或者是:("a")

So the left operand might be enclosed in brackets so maybe it should "repeat the list". The list is either: ("a") x 2 or it is: ("a")

如果重复("a") x 2,我们将得到("a") x 2 ("a") x 2.这似乎不正确.

If we repeated ("a") x 2 we would get ("a") x 2 ("a") x 2. This doesn't seem right.

如果键入:print $a[1],我们将得到一个"a",因此它重复了列表"意味着Perl将("a") x 2转换为("a", "a"),因此我们有效地得到了@a=("a", "a")

If we type: print $a[1] we will get a single 'a', so "it repeats the list" means Perl turns ("a") x 2 into ("a", "a") so we effectively get @a=("a", "a")

但是,print ("a") x 2不会变成("a", "a").这是因为print是具有较高优先级的列表运算符".这样我们就可以有效地得到:(print ("a")) x 2

However, print ("a") x 2 does not get turned into ("a", "a"). That's because print is a "list operator" with a high precedence. So there we effectively get: (print ("a")) x 2

数组是一个术语,因此它也具有较高的优先级,但是@ a = stuff涉及具有较低优先级的赋值运算符=.因此,它与印刷品完全不同.

An array is a term so it also has a high precedence, but @a=stuff involves the assignation operator = which has a relatively low precedence. So it's quite different from print.

推荐答案

您被通用的Perl解析陷阱所困扰.您的第三条语句print ("a") x 2解析为:

You are being bitten by a common Perl parsing gotcha. Your third statement print ("a") x 2 is parsed as:

(print ("a")) x 2;

您可以添加另一组括号来修复该解析:

You can add another set of parentheses to fix the parsing:

print (("a") x 2);  # prints aa

这篇关于关于优先+重复修饰符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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