散列和数组

散列和数组

数租@alldriverpins,和@alliopins是两饿不同的数组,我要将两个数组进行一些同样的操作.

PrintFailPinsToFile($tester,$not_used_pins,$avg,$simga,"append",$failed_file,@alldriverpins,%information_driver_io);

.......................

PrintFailPinsToFile($tester,$not_used_pins,$avg,$simga,"append",$failed_file,@alliopins,%information_driver_io);



我设置

sub PrintFailPinsToFile
{
my ($tester,$not_used_pins,$avg,$simga,$open_mode,$failed_file,@kindpins)=@_;
.....
}


kindpins数据传过来的总是不对,好象参数中同时传送数组,和散列就是不对,怎么同时船送散列和树组
用引用
如果直接传送

[Copy to clipboard] [ - ]
CODE:
func(%hash)

这样的代码会把hash的key和value展开成一个一维数组的
什么意思,我现在就是要同时传送数和散列,怎么同时传送,并且在传送的数组中,名字要修改
用两个引用~~
引用,相当于把你的散列数组都存在一个变量里,然后就当普通标量一样传递。使用时再展开
# more fun.pl
#!/usr/bin/perl -w

my %hash1 = {"a"=> "333",
             "b"=> "666",
             "c"=>"999",};
my $a=func(%hash1);
print "$a\n";

出鑤# fun.pl
Reference found where even-sized list expected at /root/perl/fun.pl line 3.
Undefined subroutine &main::func called at /root/perl/fun.pl line 6.

请指点一下呢


QUOTE:
原帖由 bitterness 于 2008-12-4 16:12 发表
# more fun.pl
#!/usr/bin/perl -w

my %hash1 = {"a"=> "333",
             "b"=> "666",
             "c"=>"999",};
my $a=func(%hash1);
print "$a\n";

出鑤# fun.pl
Reference found where ev ...

1 {..} 创建了一个匿名散列, 不能直接赋值给%hash1.
   初始化%hash1 用 ()
2 func未定义。


[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
use strict;
use warnings;
sub func {
  #Simplily print out all the arguments
  my ($array_ref,$hash_ref) = @_;
  print "Element of array are:", @$array_ref,"\n";
  print "Element of hash are:\n";
  print "$_ -> ",$hash_ref->{$_},"\n" foreach (keys %$hash_ref);
}

my $hash = {
    "a" => 1,
    "b" => 2,
};
my $array = [4,5,6];
func($array,$hash);



QUOTE:
原帖由 churchmice 于 2008-12-4 18:10 发表

#!/usr/bin/perl
use strict;
use warnings;
sub func {
  #Simplily print out all the arguments
  my ($array_ref,$hash_ref) = @_;
  print "Element of array are:", @$array_ref,"\n";
  print  ...

刚才说了my %hash = {}是错误的,除了%hash = (), 还可以像楼上(借用一下 )那样创建一个匿名散列的引用

[Copy to clipboard] [ - ]
CODE:
my $hash = {
    "a" => 1,
    "b" => 2,
};