Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在perl中打印出散列数组_Arrays_Perl_Hash_Printing - Fatal编程技术网

Arrays 在perl中打印出散列数组

Arrays 在perl中打印出散列数组,arrays,perl,hash,printing,Arrays,Perl,Hash,Printing,因此,我有以下代码: while (my $line = <$data>){ my $fields = split /,/, $line; my $name = fields[0]; my $nickname = fields[1]; my $occurences = fields[2]; my $number = fields[3]; # I need to push it like this so that $occurences are i

因此,我有以下代码:

while (my $line = <$data>){
   my $fields = split /,/, $line;
   my $name = fields[0];
   my $nickname = fields[1];
   my $occurences = fields[2];
   my $number = fields[3];

   # I need to push it like this so that $occurences are in numerical order, i.e: 0,1,2,....,10
   push @{$hash{$name}{$nickname}}, {$occurences=>$number};  
}

for my $name (keys %hash) {
  for my $nickname (keys %{$hash{$name}}) {
    for my $value (keys @{$hash{$name}{$nickname}}) {
      print "Value is Occurences: $value\n";
    }
  }
}
预期产出:

Value is Occurences: 1
 Number is: 10
Value is Occurences: 1
 Number is: 9
Value is Occurences: 1
 Number is: 2
Value is Occurences: 2
 Number is: 3
 .
 .
 .

“Value is occurrencess:#”,则#应按递增顺序排列。

也许下面的代码演示了如何实现所需的结果

use strict;
use warnings;
use feature 'say';

my @fields = split ',', <DATA>;
my %hash;

while(<DATA>) {
    chomp;
    my($name,$nickname,$occurence,$number) = split ',';
    push @{$hash{$name}{$nickname}{$occurence}},$number;
}

for my $name (keys %hash) {
  for my $nickname (keys %{$hash{$name}}) {
    for my $occurence (sort {$a <=> $b} keys %{$hash{$name}{$nickname}}) {
      say "$name:$nickname:$occurence:"
        . join(',',sort {$a <=> $b} @{$hash{$name}{$nickname}{$occurence}});
    }
  }
}

__DATA__
Name,Nickname,Occurences,Number
BobWill,bob,1,10
BobWill,bob,1,9
BobWill,bob,1,2
BobWill,bob,2,3
BobWill,bob,6,5
BobWill,bob,3,7
BobWill,will,1,10
BobWill,will,1,9
BobWill,will,1,2
BobWill,will,2,3
BobWill,will,3,5
BobWill,will,3,7
Danny,dan,10,2
Danny,dan,1,3
Danny,dan,8,9

也许下面的代码演示了如何实现期望的结果

use strict;
use warnings;
use feature 'say';

my @fields = split ',', <DATA>;
my %hash;

while(<DATA>) {
    chomp;
    my($name,$nickname,$occurence,$number) = split ',';
    push @{$hash{$name}{$nickname}{$occurence}},$number;
}

for my $name (keys %hash) {
  for my $nickname (keys %{$hash{$name}}) {
    for my $occurence (sort {$a <=> $b} keys %{$hash{$name}{$nickname}}) {
      say "$name:$nickname:$occurence:"
        . join(',',sort {$a <=> $b} @{$hash{$name}{$nickname}{$occurence}});
    }
  }
}

__DATA__
Name,Nickname,Occurences,Number
BobWill,bob,1,10
BobWill,bob,1,9
BobWill,bob,1,2
BobWill,bob,2,3
BobWill,bob,6,5
BobWill,bob,3,7
BobWill,will,1,10
BobWill,will,1,9
BobWill,will,1,2
BobWill,will,2,3
BobWill,will,3,5
BobWill,will,3,7
Danny,dan,10,2
Danny,dan,1,3
Danny,dan,8,9

将示例数据与
\uuuuuu数据一起使用如何?为什么要在数组上使用
?如何将示例数据与
\uuuuuu数据
一起使用?为什么要在数组上使用
BobWill:will:1:2,9,10
BobWill:will:2:3
BobWill:will:3:5,7
BobWill:bob:1:2,9,10
BobWill:bob:2:3
BobWill:bob:3:7
BobWill:bob:6:5
Danny:dan:1:3
Danny:dan:8:9
Danny:dan:10:2