散列赋值中的奇数元素,默认情况下 [英] Odd number of elements in hash assignment with default
问题描述
问候亲爱的社区.p>
我正在尝试在 perl 中创建一个 sub
,它接受一个哈希值和一个默认为零的调试标志.但是我不断收到此错误哈希分配中元素的奇数
.如果我不使用调试标志,它似乎工作.
感谢大家的帮助.
代码:
#!/usr/bin/perl使用严格;使用警告;使用 Getopt::Long;使用 POSIX qw(strftime);##file2hash : 读取 k 中的文件例如=kconfig &kmem 进入哈希表#子文件2哈希{我的 ($file) = @_;open(my $data, '<', $file) or die "Could not open '$file' $!\n";我的 %HoH;我的 $key;我的价值;我的 $who;我的 $rec;我的 $field;#while ( 我的 $line = <$data>) {while ( <$data>) {#print $line;下一个除非 (s/^(.*?):\s*//);#/关闭编辑器着色$who = $1;#print $who;$rec = {};$HoH{$who} = $rec;对于 $field ( split ) {($key, $value) = split/=/, $field;$rec->{$key} = $value;}}返回 %HoH;}##end file2hash###打印出 k 中的哈希表格式#子 hash2print{(我的 %HoH,我的 $debug)= @_;#my ($debug)=@_||0;#my %HoH = 班次;#my $debug = shift ||0;我的$家人;我的$角色;对于 $family ( 键 %HoH ) {#print "$family\n";对于 $role ( 键 %{ $HoH{$family} } ) {如果($调试){打印家庭:$家庭\n";打印角色:$角色\n";}打印$role=$HoH{$family}{$role}";}打印\n";}}##end hash2print#分派{我的 $inc= shift;我的 $config_f = shift ||"kconfig";我的 $memory_f = shift ||公里";我的 %h2=&file2hash($config_f);我的 %m2=file2hash($memory_f);我的 $today=&getDate();打印$今天\n";打印 "$inc\n";我的 $inc_cnt = $m2{$today}{$inc} ||-999999999;打印$inc_cnt\n";#my %config = shift;#my %mem = shift;#my $event = shift;#print $m2{$inc}{$today};}子获取日期{我的 $date = strftime "%m/%d/%Y", localtime;#"#print $date;返回 $date;}我的 %h2=&file2hash("kconfig");我的 %m2=&file2hash("kmem");&hash2print(%h2,1);&hash2print(%m2,1);#print &getDate();#my $xcnt= &dispatch("event_c3_z2");#&dispatch("event_c3_z2");#print $xcnt;
测试文件1:
event_a1_x1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=6event_a1_x2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=5event_b2_y1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=4event_b2_y2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=3event_c3_z1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=2event_c3_z2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=1
测试文件2:
201609230012:event_a1_x1=6201609230744:event_a1_x2=5201609230844:event_b2_y1=4201609230342:event_b2_y2=3201609230245:event_c3_z1=2201609230100:event_c3_z2=1
您想按值将散列值传递给您的子程序,这就产生了问题.尝试通过引用传递您的哈希 %h2
(注意 %
之前的 \
):
&hash2print(\%h2, 1);
然后在您的子 hash2print
中,您可以通过以下方式获取散列:
sub hash2print {(我的 $hashref, 我的 $debug) = @_;我的 %HoH = %$hashref;# 取消引用 $hashref 以取回哈希...
如果您不了解参考文献背后的概念,您可以在此处阅读更多有关参考文献的信息.
Greeting Dear Community.
I'm trying to make a sub
in perl that takes a hash and a debug flag which defaults to zero. However I keep getting this error Odd number of elements in hash assignment
. If I don't use the debug flag, it seems to work.
Thanks for all the help.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use POSIX qw(strftime);
#
#file2hash : read the file in k<file_name> e.g.=kconfig & kmem into hash table
#
sub file2hash {
my ($file) = @_;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
my %HoH;
my $key;
my $value;
my $who;
my $rec;
my $field;
#while ( my $line = <$data>) {
while ( <$data>) {
#print $line;
next unless (s/^(.*?):\s*//); # / turn off editor coloring
$who = $1;
#print $who;
$rec = {};
$HoH{$who} = $rec;
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
}
return %HoH;
}
#
#end file2hash
#
#
#print out hash table in k<file_name> format
#
sub hash2print{
(my %HoH,my $debug) = @_;
#my ($debug)=@_||0;
#my %HoH = shift;
#my $debug = shift || 0;
my $family;
my $role;
for $family ( keys %HoH ) {
#print "$family\n";
for $role ( keys %{ $HoH{$family} } ) {
if ($debug){
print "family:$family\n";
print "role: $role\n";
}
print "$role=$HoH{$family}{$role}";
}
print "\n";
}
}
#
#end hash2print
#
sub dispatch{
my $inc= shift;
my $config_f = shift || "kconfig";
my $memory_f = shift || "kmem";
my %h2=&file2hash($config_f);
my %m2=file2hash($memory_f);
my $today=&getDate();
print "$today\n";
print "$inc\n";
my $inc_cnt = $m2{$today}{$inc} || -999999999;
print "$inc_cnt\n";
#my %config = shift;
#my %mem = shift;
#my $event = shift;
#print $m2{$inc}{$today};
}
sub getDate{
my $date = strftime "%m/%d/%Y", localtime; # "
#print $date;
return $date;
}
my %h2=&file2hash("kconfig");
my %m2=&file2hash("kmem");
&hash2print(%h2,1);
&hash2print(%m2,1);
#print &getDate();
#my $xcnt= &dispatch("event_c3_z2");
#&dispatch("event_c3_z2");
#print $xcnt;
Test file1:
event_a1_x1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=6
event_a1_x2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=5
event_b2_y1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=4
event_b2_y2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=3
event_c3_z1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=2
event_c3_z2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=1
test file2:
201609230012: event_a1_x1=6
201609230744: event_a1_x2=5
201609230844: event_b2_y1=4
201609230342: event_b2_y2=3
201609230245: event_c3_z1=2
201609230100: event_c3_z2=1
You want to pass your hashes by value to your subroutine and this creates the problem. Try to pass your hash %h2
by reference instead (notice the \
before the %
):
&hash2print(\%h2, 1);
Then in your sub hash2print
, you can get the hash back in the following way:
sub hash2print {
(my $hashref, my $debug) = @_;
my %HoH = %$hashref; # dereference $hashref to get back the hash
...
You can read more about references here if you don't understand the concepts behind them.
这篇关于散列赋值中的奇数元素,默认情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!