2012-04-15 27 views
2

我不是Perl的專家,所以這可能是一個簡單的問題。如何使用Perl耐儲藏,改變的哈希值

我一直在使用耐儲藏,並按照this example存儲哈希。首先,我存儲原始散列。

use Storable qw(store retrieve freeze thaw dclone); 
%color = ('Blue' => 1, 'Red' => 0.8, 'Black' => 0, 'White' => 1); 
store(\%color, 'mycolors'); 

然後我檢索它。 (不同的腳本)

use Storable qw(store retrieve freeze thaw dclone); 
$colref = retrieve('mycolors'); 
printf "Blue is still %lf\n", $colref->{'Blue'}; 

我的問題是我該如何改變其中一個哈希值?例如,在第二個腳本中執行如下操作:

$colref->{'Blue'} = 2; 
store(\%color, 'mycolors'); 

+0

當你嘗試過什麼事? – geekosaur 2012-04-15 19:16:42

+0

@geekosaur每當我讀回來,藍色(和所有其他顏色)現在是0.我認爲這是有道理的 - %顏色沒有在讀腳本定義,但我不知道我應該如何拉它from'mycolors' – varatis 2012-04-15 19:20:30

+0

你有正確的想法;考慮一下你已經擁有的東西,與你想傳遞給'store'的東西相比。 – geekosaur 2012-04-15 19:23:12

回答

6

需要改變

store(\%color, 'mycolors'); 

store($colref, 'mycolors');