Arrays 在Perl中,将数组推送到数组中的某个哈希
我想在Perl中动态地将哈希值推送到哈希数组中 我有这个代码块来创建类哈希并将其推送到数组类列表中Arrays 在Perl中,将数组推送到数组中的某个哈希,arrays,perl,push,hash,Arrays,Perl,Push,Hash,我想在Perl中动态地将哈希值推送到哈希数组中 我有这个代码块来创建类哈希并将其推送到数组类列表中 $courseName = <STDIN>; $section = <STDIN>; my $classHash = {}; $classHash->{courseName} = $courseName; $classHash->{section} = $section; push @classList, $c
$courseName = <STDIN>;
$section = <STDIN>;
my $classHash = {};
$classHash->{courseName} = $courseName;
$classHash->{section} = $section;
push @classList, $classHash;
$courseName=;
$section=;
我的$classHash={};
$classHash->{courseName}=$courseName;
$classHash->{section}=$section;
推送@classList,$classHash;
现在,我想在类哈希中添加一个studentHash
for my $i ( 0 .. $#classList ) {
#I want to add the studentHash to a specific classHash in the classList
if($courseName1 eq $classList[$i]{courseName} && $section1 eq $classList[$i]{section}){
$studName = <STDIN>;
$studNum = <STDIN>;
my $studHash = {};
$studHash->{studName} = $studName;
$studHash->{studNum} = $studNum;
push @studList, $studHash;
push @{$classList[$i]}, \@studList; #but this creates an array reference error
}
}
用于我的$i(0..$#类列表){
#我想将studentHash添加到类列表中的特定类哈希中
if($courseName1 eq$classList[$i]{courseName}&$section1 eq$classList[$i]{section}){
$studName=;
$studNum=;
我的$studHash={};
$studHash->{studName}=$studName;
$studHash->{studNum}=$studNum;
推送@studList,$studHash;
push@{$classList[$i]},\@studList;\但这会创建数组引用错误
}
}
忽略交互位。。。以下是如何将学生添加到类中:
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
my @classList = (
{
courseName => 'Algebra',
section => 101,
students => [],
},
{
courseName => 'Geometry',
section => 102,
students => [],
},
);
my $studName = 'Alice';
my $studNum = 13579;
my $desiredClass = 'Geometry';
my $desiredSection = 102;
for my $class (@classList) {
if ($class->{courseName} eq $desiredClass and
$class->{section} eq $desiredSection) {
# Add student to the class
my $student = {
studName => $studName,
studNum => $studNum,
};
push @{ $class->{students} }, $student;
}
}
print Dumper \@classList;
# Printing out the students for each class
for my $class (@classList) {
my $course = $class->{courseName};
my $section = $class->{courseSection};
my $students = $class->{students};
my $total_students = scalar @$students;
my $names = join ', ', map { $_->{studName} } @$students;
print "There are $total_students taking $course section $section.\n";
print "There names are [ $names ]\n";
}
输出
VAR1 = [
{
'students' => [],
'section' => 101,
'courseName' => 'Algebra'
},
{
'students' => [
{
'studNum' => 13579,
'studName' => 'Alice'
}
],
'section' => 102,
'courseName' => 'Geometry'
}
];
There are 0 students taking Algebra section 101.
There names are [ ]
There are 1 students taking Geometry section 102.
There names are [ Alice ]
添加$classHash->{students}=[];然后将$studHash推入@{$classList[$i]->{students}中。studList数组只能存储在现有的classHash中(在第一个代码块上创建)。我不明白为什么要添加classHash->{students}=[];是的,因此您可以通过向%classHash添加
students
字段为学生腾出空间,然后将$studHash添加到该数组中。也许可以提供一个示例,说明您希望在末尾使用的数据结构它不再导致错误,但它所做的是将studHash添加到所有现有的classHash中。我只想将studHash添加到特定的类hash中。如何访问类中的每个学生?代码可以工作,但我不知道如何按类哈希打印studList