Learning Perl: 8.5. Interpolating into Patterns

Previous Page
Next Page

 

8.5. Interpolating into Patterns

The regular expression is double-quote interpolated as if it were a double-quoted string. This allows us to write a quick grep-like program like this:

    #!/usr/bin/perl -w
    my $what = "larry";

    while (<>) {
      if (/^($what)/) {  # pattern is anchored at beginning of string
        print "We saw $what in beginning of $_";
      }
    }

The pattern will be built up out of whatever's in $what when we run the pattern match. In this case, it's the same as if we had written /^(larry)/, looking for larry at the start of each line.

We didn't have to get the value of $what from a literal string and could have gotten it from the command-line arguments in @ARGV:

    my $what = shift @ARGV;

If the first command-line argument is fred|barney, the pattern becomes /^(fred|barney)/, looking for fred or barney at the start of each line.[] The parentheses (which weren't necessary when searching for larry) have become important because without them we'd be matching fred at the start or barney anywhere in the string.

[] The astute reader will know that you can't generally type fred|barney as an argument at the command line because the vertical bar is a shell metacharacter. See the documentation to your shell to learn about how to quote command-line arguments.

With that line changed to get the pattern from @ARGV, this program resembles the Unix grep command. But we have to watch out for metacharacters in the string. If $what contains 'fred(barney', the pattern will look like /^(fred(barney)/. You know that can't workit'll crash your program with an invalid regular expression error. With some advanced techniques,[*] you can trap this kind of error (or prevent the magic of the metacharacters in the first place) so it won't crash your program. But for now, just know that if you give your users the power of regular expressions, they'll need the responsibility to use them correctly.

[*] In this case, you would use an eval block to trap the error, or you would quote the interpolated text using quotemeta (or its /Q equivalent form) so it's no longer treated as a regular expression.

Previous Page
Next Page
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
这些错误信息表明在项目路径为"D:/HFSStemp/"的Project2中,使用了DrivenModal的TeeModal模式进行仿真时出现了一些问题。根据错误信息可以看出: 1. [info] Simulation for Setup1 is already running. To resimulate stop prior simulation of this setup. (10:38:30 下午 7月 14, 2023) 这个信息表示在同一个设置(Setup1)上已经有一个仿真正在运行,如果要重新仿真,需要停止之前的仿真。 2. [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) 这个错误指示尝试通过MPI启动求解器时失败,可能是由于未在一台或多台机器上安装供应商MPI或在启动尝试期间MPI的密码验证失败。 3. [info] An interpolating frequency sweep with 451 points has been started using HFSS - Solving Distributed. (10:40:23 下午 7月 14, 2023) 这个信息表示使用HFSS - Solving Distributed开始进行了一个具有451个点的插值频率扫描。 4. [error] Unable to locate message 11189 in HFSS message library. (10:40:23 下午 7月 14, 2023) 这个错误提示在HFSS消息库中找不到消息11189。 5. [error] Simulation completed with execution error on server: Local Machine. (10:40:23 下午 7月 14, 2023) 这个错误表示仿真在本地机器上执行时出现错误。 根据这些错误信息,可以尝试解决问题的方法包括: - 停止之前正在运行的仿真,以便重新仿真。 - 确保已正确安装供应商MPI,并且MPI的密码验证正确。 - 检查HFSS消息库,确保消息11189存在。 - 检查本地机器上是否存在执行仿真所需的所有依赖项,并确保它们正确配置和运行。 如果问题仍然存在,可能需要进一步检查设置和代码,或者查看相关文档或论坛以获取更多关于这些错误的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值