简体   繁体   English

Perl:将标量替换为该索引的数组元素

[英]Perl: substitute scalar with element of array for that index

I have the following code 我有以下代码

    #!/bin/perl

    $prevjob[1]= 'PREVJOB1';
    $prevjob[2]= 'PREVJOB2';
    $prevjob[3]= 'PREVJOB3';
    $line[2]= '(s1&s3)|s2';
    if($line[2]) {
    #this will work for first replacement, but its not working,
            $line[2]=~ s/s([0-9])/s${prevjob[\1]}/;
            print $line[2];
    }

I want to use the numbers in the scalar as indexes and substitute the number with @prevjob's element at that index. 我想使用标量中的数字作为索引,并用该索引处的@prevjob元素替换数字。

I want to replace s/s([0-9])/s${prevjob[\\1]}/ My expected output is : 我想替换s/s([0-9])/s${prevjob[\\1]}/我的预期输出是:

(sPREVJOB1&sPREVJOB3)|sPREVJOB2

I figured this out. 我想通了。 Looks like i needed s/s([0-9])/s${prevjob[$1]}/; 看起来我需要s/s([0-9])/s${prevjob[$1]}/;

$1 instead of \\1 $1代替\\1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM