0

我有以下哈希结构

test =>  '/var/tmp $slot'

my $slot_number = 0;  # just another variable.

然后我获取键的值并存储在一个名为 $test_command 的变量中

现在我需要用另一个名为的变量替换$slotin 所以我正在尝试这个$test_command$slot_number

$test_command =~ s/$slot/$slot_number/g;  this does not work

$test_command =~ s/$slot/$slot_number/ee; does not work

$test_command =~ s/\$slot/\$slot_number/g; this does not work

预期输出应该是

$test_command = /var/tmp 0
4

2 回答 2

3

这个怎么样? $test_command=~s/\$slot/$slot_number/g;

这段代码:

my $slot_number = 5;
my $test_command = '/var/tmp $slot';
$test_command=~s/\$slot/$slot_number/g;
print "$test_command\n";

印刷:

/var/tmp 5

如果您想用值替换第二个变量,您不想转义它。

于 2012-05-09T18:16:38.503 回答
1

你这么近!看看以下是否会做你想要的:

use strict;
use warnings;

my $test_command = '/var/tmp $slot';
my $slot_number = 0;

$test_command =~ s/\$slot/$slot_number/;

print $test_command;

输出

/var/tmp 0
于 2012-05-09T18:24:05.733 回答