Perl: 3.5. Interpolating Arrays into Strings

Previous Page
Next Page

 

3.5. Interpolating Arrays into Strings

Like scalars, array values may be interpolated into a double-quoted string. Elements of an array are automatically separated by spaces[*] upon interpolation:

[*] The separator is the value of the special $" variable, which is a space by default.

    @rocks = qw{ flintstone slate rubble };
    print "quartz @rocks limestone/n";  # prints five rocks separated by spaces

There are no extra spaces added before or after an interpolated array; if you want those, you'll have to put them in yourself:

    print "Three rocks are: @rocks./n";
    print "There's nothing in the parens (@empty) here./n";

If you forget that arrays interpolate like this, you'll be surprised when you put an email address into a double-quoted string. For historical reasons,[] this is a fatal error at compile time:

[] Since you asked: Before Version 5, Perl would silently leave uninterpolated an unused array's name in a double-quoted string. So, "fred@bedrock.edu" might be a string containing an email address. This attempt to "Do What I Mean" will backfire when someone adds a variable named @bedrock to the program; now the string becomes "fred.edu" or worse.

    $email = "fred@bedrock.edu";  # WRONG! Tries to interpolate @bedrock
    $email = "fred/@bedrock.edu"; # Correct
    $email = 'fred@bedrock.edu';  # Another way to do that

However, in versions of Perl 5 soon to be released as we write this, the behavior of an unseen array variable will become similar to an unseen scalar variable, i.e., replaced with an empty string with a warning if warnings are enabled. The Perl developers apparently figure that 10 years of fatality are enough warning.

A single element of an array will be replaced by its value as you'd expect:

    @fred = qw(hello dolly);
    $y = 2;
    $x = "This is $fred[1]'s place";    # "This is dolly's place"
    $x = "This is $fred[$y-1]'s place"; # same thing

The index expression is evaluated as an ordinary expression, as if it were outside a string. It is not variable interpolated first. In other words, if $y contains the string "2*4", we're still talking about element 1, not element 7, because "2*4" as a number (the value of $y used in a numeric expression) is just plain 2.[*] If you want to follow a simple scalar variable with a left square bracket, you need to delimit the square bracket so it isn't considered part of an array reference:

[*] Of course, if you've got warnings turned on, Perl is likely to remind you that "2*4" is a funny-looking number.

    @fred = qw(eating rocks is wrong);
    $fred = "right";               # we are trying to say "this is right[3]"
    print "this is $fred[3]/n";    # prints "wrong" using $fred[3]
    print "this is ${fred}[3]/n";  # prints "right" (protected by braces)
    print "this is $fred"."[3]/n"; # right again (different string)
    print "this is $fred/[3]/n";   # right again (backslash hides it)

Previous Page
Next Page
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Project2 (D:/HFSStemp/) TeeModal (DrivenModal) [info] Simulation for Setup1 is already running. To resimulate stop prior simulation of this setup. (10:38:30 下午 7月 14, 2023) [error] Unable to create child process: hf3d. (10:40:23 下午 7月 14, 2023) [error] The attempted launch of solvers via MPI failed while connecting to communication pipes. The probable cause is failure to install the vendor MPI on one or more machines or password authentication failure for MPI during the launch attempt. (10:40:23 下午 7月 14, 2023) [info] An interpolating frequency sweep with 451 points has been started using HFSS - Solving Distributed. (10:40:23 下午 7月 14, 2023) [error] Unable to locate message 11189 in HFSS message library. (10:40:23 下午 7月 14, 2023) [error] Simulation completed with execution error on server: Local Machine. (10:40:23 下午 7月 14, 2023) [info] Simulation for Setup1 is already running. To resimulate stop prior simulation of this setup. (10:43:06 下午 7月 14, 2023) [error] Unable to create child process: hf3d. (10:44:35 下午 7月 14, 2023) [error] The attempted launch of solvers via MPI failed while connecting to communication pipes. The probable cause is failure to install the vendor MPI on one or more machines or password authentication failure for MPI during the launch attempt. (10:44:35 下午 7月 14, 2023) [info] An interpolating frequency sweep with 451 points has been started using HFSS - Solving Distributed. (10:44:35 下午 7月 14, 2023) [error] Unable to locate message 11189 in HFSS message library. (10:44:35 下午 7月 14, 2023) [error] Simulation completed with execution error on server: Local Machine. (10:44:35 下午 7月 14, 2023)
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值