2009年2月14日星期六

[PerlChina] Re: 询问关于自动生成hash

while (<DATA>) {
chomp;
my ( $num, $key, $val ) = split;
my $code = "%hash_$num = ('$key' => '$val')";
eval $code;
die "syntax error: $@" if $@;
# test
use Data::Dumper;
print Dumper(\%{"hash_$num"});
}

__DATA__
1 k1 v1
2 k2 v2

2009/2/15 allen <allenogz@gmail.com>:
> 那我定义一下格式: $num $name $value
> 从某一个文件中获得这3个值
> 然后根据$num的多少自动生成相应的hash
> 比如 2 张三 5
> 生成 的hash 为 hash_2 其中的key 张三 对应的值为5
> 再比如 4 李四 6 生成的hash 为hash_4 其中的key 李四 对应的值为6
>
> 于 2009-2-15 8:07, kind terry 写道:
>> lz你写的需求,不清晰啊,最好举个例子输入什么,然后需要得到什么样的hash
>>
>> 2009/2/15 allen <allenogz@gmail.com <mailto:allenogz@gmail.com>>
>>
>> perl 里面怎么获得如同C里面 allocate的那种能力?
>> 比如我输入2,就自动产生2组hash;输入3,就自动产生3组hash
>> 我现在写的脚本很需要这样的功能,希望大家能帮助我。
>> 我写的:
>>
>> my ($input_thing ,$input_value) = @_;
>>
>> #---------------------
>> # internal variables
>> #---------------------
>> my $i=0;
>> $i++;
>>
>> my ${hash_$i}{$input_thing}={$input_value};
>>
>>
>> 这样的话 报错。。。。。。。
>>
>>
>>
>>
>> >
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

没有评论: