Arrays 如何将数组变量指定为哈希?

Arrays 如何将数组变量指定为哈希?,arrays,perl,multidimensional-array,hash,key,Arrays,Perl,Multidimensional Array,Hash,Key,我正在打印这个散列。由于key1是array[0],key2是array[2],$sum[0]是值。但has不起作用。我做错了什么 @array=(10,45,20); @sum=($array[0]+$array[1]+$array[2]); %hash; $hash{$array[0]}{$array[2]}=$sum[0] 在散列的末尾,我想将10:75打印到屏幕上。 始终使用严格使用;使用警告qw(全部) 一次只有一个和,所以不需要数组 不需要散列的散列;一个简单的散列就可以了

我正在打印这个散列。由于
key1
array[0]
key2
array[2]
$sum[0]
是值。但has不起作用。我做错了什么

@array=(10,45,20);

@sum=($array[0]+$array[1]+$array[2]);

%hash;

$hash{$array[0]}{$array[2]}=$sum[0]
在散列的末尾,我想将
10:75
打印到屏幕上。

  • 始终使用
    严格使用;使用警告qw(全部)
    
  • 一次只有一个和,所以不需要数组
  • 不需要散列的散列;一个简单的散列就可以了
固定的:

use strict;
use warnings qw( all );

use List::Util qw( sum );


my %hash;
while (...) {
   my @nums = ...;
   $hash{ $nums[0] } = sum(@nums);
}

for (sort { $a <=> $b } keys(%hash)) {
   print("$_: $hash{$_}\n");
}
使用严格;
使用警告qw(全部);
使用列表::Util qw(总和);
我的%hash;
而(…){
我的@nums=。。。;
$hash{$nums[0]}=sum(@nums);
}
对于(排序{$a$b}键(%hash)){
打印(“$\:$hash{$\}\n”);
}
您已设置

$hash{$array[0]}{$array[2]} = $sum[0]
具有给定值的

$hash{10}{20} = 75
如果要从散列中打印
10:75
,则需要编写

printf "%d : %d\n",10, $hash{10}{20}

虽然我确信你想要更一般的东西,但你确实没有从你对@ikegami«我的程序将接受…»的描述中提供足够的信息。我创建了一个包含以下数据的文件:

数据_1.txt:

请注意,实际上只有前两行与描述相匹配,稍后我会再讨论这一点

sum.pl:

请注意,严格来说,while循环中的块可以写成:

    chomp $line;
    my ($name, $grade_1, $grade_2, $grade_3) = split / /, $line;
    $sum = $grade_1 + $grade_2 + $grade_3;
    $sums_and_names{$sum}{$name} = [ $grade_1, $grade_2, $grade_3 ];
但我引用@Borodin的话:

虽然我确信你想要的是更一般的东西,但你确实没有提供足够的信息



请始终添加
使用严格;使用警告编码到脚本中以避免常见错误。//你到底有什么问题?您创建了一个包含
{'10'=>{'20'=>75}
的哈希。你想要什么呢?在散列结束时,我想在屏幕上打印10:75,但我不知道如何给出这个散列的引用。那你为什么要使用二维散列呢?为什么不干脆
$hash{$array[0]}=$sum[0]
?这就给出了
{'10'=>75}
。是的,我知道。我从一个文本中获取值并放入一个数组。然后将这个数组做成一个二维散列,带有两个键,其中数组的第一个元素是第一个键,第二个元素是第二个键。然后我要打印它。这就是我正在尝试做的。欢迎来到计算机编程世界正如您可能已经注意到的,Borodin和ikegami试图帮助您解决一个不完整的问题描述。通常情况下,这两个人能够很好地从一半的问题中理解问题,并且愿意带你完成澄清问题的步骤我愚蠢到可以再试一次——我不比他们更清楚!!!感谢您的关注,但我已经用一个键做了,我也想用两个键创建。。您的代码只有一个键..应该是这样的。没有必要创建比所需更复杂的数据结构。chris:HASH(0x5581f6193078)我的输出似乎与我写的一样,为什么得到哈希结果..我的程序将接受两个未排序的文件。每个文件包含4列,以空格分隔。第一列是名称(字符串)。其余列(3列)是表示等级的整数。将第一个文件加载到二维散列数据结构中,整数(总等级)之和作为第二个维度键和名称。如果您的问题有问题,请修复它。如果您已经这样做了,请随时在这里留下评论,让我知道。是的,我可以这样做,但我想用它来处理大文件,所以我不能像那样写哈希,我必须像数组[1]和数组[2]那样写密钥。。。我怎么能这样接受呢?想象一下,我有一个包含chris 10 34 56个值的文件。我想把tehm作为一个数组,然后用两个键进行二维Nol哈希。chris=第一个键,数字之和=第二个键,然后ı想打印chris=树数之和。@Fatih:是的,我想象你有不止一行数据,但你的例子太琐碎了。如果你只显示一条线,我们就看不出它应该如何缩放。请再显示几行数据以及所需的相应输出。如果从未使用发送键
20
,则没有理由将其存储在散列中。@Faith:您确实需要解释为什么您认为需要将
$array[2]
作为散列键。在您的代码中,数字的总和是散列值,看起来您只需要一个键。@Fatih:错误。但是你不可能说出你真正的需求是什么,因为你只是不给我们任何信息。我投票结束你的问题。你不能使用
$filename\u 2
。不过,我不认为这有什么区别。目前还不清楚OP真正想要什么样的数据结构,他的评论也没有帮助。一、 例如,现在认为他要求的结构是
$sums\u和{$name}{$sum}
(键交换)。我们只是不知道。他应该显示所需的输出(以Data::Dumper或JSON格式),但他拒绝了。当然,我知道我没有使用
$filename_2
,但这是他最后一次描述中所说的“…接受两个未排序的文件。”---我故意把它放在那里,试图表明这是一个描述糟糕的问题,OP应该重新考虑他下次怎么问它我本来可以添加一些检查,确保其中一个确实传入了两个文件名是的,这是我的文档与之完全相似的工作。很抱歉,我无法共享代码,因为我没有权限。我只是不明白我做错了什么,通过您的示例,我明白了。谢谢。我提供的是哈希引用,而不是值。
use strict;
use warnings;

use List::Util 'sum';

# get the two filenames it should work with
#
my $filename_1 = shift;
my $filename_2 = shift;

# be sure we read a file for most modern systems, UTF-8
#
open( my $file1, '<:encoding(UTF-8)', $filename_1)
    or die "Can't open file: $filename_1";

# create the (empty) data structure
#
my %sums_and_names;
#
# the % in perl means you are talking about a hash,
# use a sensible name instead of 'hash'

# read line by line
while ( my $line = <$file1> ) {

    chomp $line; # get rid of the line endings
    my ($name, @grades) = split ' ', $line;
    #
    # this is not strictly doing what you asked for, just more flexible
    #
    # split on ' ', a space character, splits on any asmount of (white) space
    # your task said that there is one space.
    # strictly, you could should split on / /, the regular expression
    #
    # the first part will go into the variable $name, the rest in array @grades
    # strictly you have only three grades so the following would do
    # my ($name, $grade_1, $grade_2, $grade_3) = split / /, $line;

    my $sum = sum(@grades) // 'no grades';
    #
    # since we now can handle any number of grades, not just three, we could
    # have no grades at all and thus result in `undef`
    #
    # using the function sum0 would return the value 0 instead
    #
    # you'll get away with the `undef` using in a hash assignment,
    # it will turn it into an empty string `''`

=pod

    $sums_and_names{foo}{bar} = [ 'baz', 'qux' ];

=cut

    #
    # here is where your task doesn't make sense
    # i am guessing:
    #
    $sums_and_names{$sum}{$name} = \@grades;
    #
    # at least we have all the data from filename_1, and the sum of the grades

}

# please decide on what you want to print

use Data::Dumper;
print Dumper \%sums_and_names;
$VAR1 = {
          'no grades' => {
                    'lazy' => []
                         },
          '23' => {
                    'bob'  => [
                                '8',
                                '12',
                                '3'
                              ]
                  },
          '57' => {
                    'alex' => [
                                '30',
                                '15',
                                '12'
                              ]
                  },
          '62' => {
                    'will' => [
                                '06',
                                '56'
                              ]
                  },
          '75' => {
                    'john' => [
                                '10',
                                '45',
                                '20'
                              ]
                  },
          '99' => {
                    'pete' => [
                                 '23',
                                 '45',
                                 '10',
                                 '21'
                               ]
                   }
        };
    chomp $line;
    my ($name, $grade_1, $grade_2, $grade_3) = split / /, $line;
    $sum = $grade_1 + $grade_2 + $grade_3;
    $sums_and_names{$sum}{$name} = [ $grade_1, $grade_2, $grade_3 ];