【发布时间】:2014-10-06 23:04:00
【问题描述】:
如果我使用类似这样的简单方法替换另一个字符串
my $pet = "I have a dog.";
my $search = "dog";
my $replace = "cat";
$pet =~ s/$search/$replace/;
它工作正常,我得到“我有一只猫。”正如预期的那样。
但是当我使用更复杂的东西时,它不会被替换:
my $image_correction_hash = {};
$device = "my_device";
$correction_hash->{$device}->{'to_be_replaced'} = "174_4492_232313_7078721ec0.jpg";
# my json string
my $json = '[{"credits":[],"issue":174,"layout":"special_cover","text":[],"hide_overline":"","category":"Kunst","id":"174_4492","media_data":[{"thumbnail":"","data_is_cover":1,"subheadline":"","value":"174_4492.jpg","type":"image","headline":""},{"data_position":"left","thumbnail":"","subheadline":"","value":"174_4492_232302_3980b3da34.jpg","data_effect":"smear","type":"image","headline":""},{"data_position":"right","thumbnail":"","subheadline":"","value":"174_4492_232313_7078721ec0.jpg","data_effect":"smear","type":"image","headline":""}],"links":[],"textmarker":"","teaser":"","hide_headline":"","article_thumbnail":"174_4492_article_thumbnail.jpg","subheadline":"","gallery":[],"overline":"","headline":"Covertitel\n"}]';
print STDERR "JSON string before:" . $json . "\n";
foreach my $search ( keys %{$correction_hash->{$device}})
{
print STDERR "to be replaced:".$correction_hash->{$device}->{$search}.".\n";
# the replacement
$json =~ s/$search/XXXXX/g;
}
print STDERR "JSON string after:" . $json . "\n"; # no replacement occured - GRRR
这里的错误在哪里?
【问题讨论】: