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

时间:2017-12-30 18:03:42

标签: arrays perl multidimensional-array hash key

我正在尝试打印此哈希。由于key1array[0]key2array[2]$sum[0]是值。但是没有用。我做错了什么?

@array=(10,45,20);

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

%hash;

$hash{$array[0]}{$array[2]}=$sum[0]

在哈希结束时,我想将10 : 75打印到屏幕上。

3 个答案:

答案 0 :(得分:2)

你已经设置了

$hash{$array[0]}{$array[2]} = $sum[0]

具有给定值的是

$hash{10}{20} = 75

如果要从哈希中打印10 : 75,则需要编写

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

虽然我确定你想要比这更通用的东西,但你真的没有提供足够的信息

答案 1 :(得分:0)

  • 始终使用use strict; use warnings qw( all ); !!!
  • 只有一个总和(一次),所以不需要数组。
  • 不需要哈希哈希;一个简单的哈希就可以了。

修正:

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");
}

答案 2 :(得分:0)

从您给@ikegami的描述«我的程序将接受...»我创建了一个具有数据的文件:

data_1.txt

john 10 45 20
alex 30 15 12
pete 23 45 10 21
will 06 56
bob   8 12  3
lazy 

请注意,只有前两行实际上与描述相符,我稍后会再回过头来看。

sum.pl

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;

并且运行perl sum.pl data_1.txt data_2.txt将为您提供类似

的内容

输出

$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'
                               ]
                   }
        };

请注意,严格来说,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:

  

虽然我确定你想要比这更通用的东西,但你真的没有提供足够的信息