I want to search a sentence and replace it with the value of hash that matched string like this:
my $sentence = "abc def hello hi";
$sentence =~ s/abc def/$hash{'abc def'}/g;
I am not getting the correct output. Can any one please help me?
这对我有用:
#!/usr/bin/env perl
use strict;
use warnings;
my %hash = ( 'abc def' => 'pqr xyz' );
my $sentence = "abc def hello hi";
$sentence =~ s/abc def/$hash{'abc def'}/g;
print "$sentence\n";
运行时,它会打印:
pqr xyz hello hi
如果这不是你所期望的,你在期待什么?(请注意,问题中 Perl 代码的原始版本中有许多拼写错误;我认为它们是偶然的,但也许它们是您的问题的关键。使用use strict;
和use warnings;
帮助发现诸如拼写错误的变量名之类的问题。)