2

I want to dynamically push values of hashes into an array of hashes in Perl.

I have this code block to create and push classHash to an array classList.

    $courseName = <STDIN>;
    $section = <STDIN>;

    my $classHash = {};

    $classHash->{courseName} = $courseName;
    $classHash->{section} = $section;
    push @classList, $classHash;

Now, I want to add a studentHash to the classHash.

    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
        }
    }
6
  • Add $classHash->{students} = []; and then push your $studHash into @{ $classList[$i]->{students} } Commented Sep 2, 2015 at 23:39
  • The studList array shall only be stored in an existing classHash (which was created on the first block of code). I don't get why I should add classHash->{students} = []; Commented Sep 2, 2015 at 23:46
  • Yes, so you can make room for the students by adding a students field to your %classHash, and then add the $studHash to that array. Commented Sep 2, 2015 at 23:51
  • maybe provide an example of the data structure you want to have in the end Commented Sep 2, 2015 at 23:53
  • It doesn't cause an error anymore, but what it does is add the studHash to all existing classHash. I want to add a studHash to a specific classHash only. Commented Sep 2, 2015 at 23:59

1 Answer 1

1

Ignoring the interactive bits... here is how you can add the student to the class:

#!/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";
}

Output

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 ]
Sign up to request clarification or add additional context in comments.

1 Comment

How to access each students in a class? The code works but I don't know how to print the studList per classHash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.