我是 Perl 世界的新手,我需要有关从文件中的哈希保存数据以及在另一个哈希中使用它之后的帮助。
这是我的代码的一个简短示例:
#!/usr/local/bin/perl
use FileHandle;
use File::Copy;
use Data::Dumper qw(Dumper);
use Storable;
use warnings;
use strict;
my $user_data;
$user_data->{test_user}->{1256489043}->{STATUS} = "RUN";
$user_data->{test_user}->{1256489043}->{MEM} = "51591";
$user_data->{test_user}->{1256489043}->{RUN_TIME} = "41410";
$user_data->{test_user}->{1256489043}->{PROJ_NAME} = "unkown";
$user_data->{test_user}->{1256489043}->{GROUP} = "default";
$user_data->{test_user}->{1256489043}->{DATE} = "Aug 17 05:23";
$user_data->{test_user_2}->{528562752}->{STATUS} = "RUN";
$user_data->{test_user_2}->{528562752}->{MEM} = "591";
$user_data->{test_user_2}->{528562752}->{RUN_TIME} = "46410";
$user_data->{test_user_2}->{528562752}->{PROJ_NAME} = "unkown";
$user_data->{test_user_2}->{528562752}->{GROUP} = "default";
$user_data->{test_user_2}->{528562752}->{DATE} = "Aug 17 05:23";
store (\$user_data, 'temp_jobs.txt') or die "can't store data to $!";
my $data = retrieve('temp_jobs.txt');
print "Hash 1\n";
print Dumper \$user_data;
print "Hash2\n";
print Dumper \$data;
my @new_array_id;
foreach my $user (keys %{$user_data}) {
foreach my $job_id (keys %{$user_data->{$user}}) {
push (@new_array_id, $job_id)
}
}
my @old_array_id;
foreach my $user (keys %{$data}) {
foreach my $job_id (keys %{$data->{$user}}) {
push (@old_array_id, $job_id)
}
}
这些是 Dumper 打印的输出:
$VAR1 = \{
'test_user_2' => {
'528562752' => {
'GROUP' => 'default',
'PROJ_NAME' => 'unkown',
'DATE' => 'Aug 17 05:23',
'STATUS' => 'RUN',
'RUN_TIME' => '46410',
'MEM' => '591'
}
},
'test_user' => {
'1256489043' => {
'GROUP' => 'default',
'PROJ_NAME' => 'unkown',
'DATE' => 'Aug 17 05:23',
'STATUS' => 'RUN',
'RUN_TIME' => '41410',
'MEM' => '51591'
}
}
};
$VAR1 = \\{
'test_user' => {
'1256489043' => {
'GROUP' => 'default',
'PROJ_NAME' => 'unkown',
'DATE' => 'Aug 17 05:23',
'STATUS' => 'RUN',
'RUN_TIME' => '41410',
'MEM' => '51591'
}
},
'test_user_2' => {
'528562752' => {
'GROUP' => 'default',
'PROJ_NAME' => 'unkown',
'DATE' => 'Aug 17 05:23',
'STATUS' => 'RUN',
'RUN_TIME' => '46410',
'MEM' => '591'
}
}
};
在第二个 foreach 循环中观察到错误:
foreach my $user (keys %{$data}) {
foreach my $job_id (keys %{$data->{$user}}) {
push (@old_array_id, $job_id)
}
}
输出:不是 report.pl 的 HASH 引用
实际上,我不确定检索到的数据的类型。您能帮忙将数据保存在新的哈希中吗?
最好的问候, SK