push into hashs of arrays

J

Jie

Hi,

I have a STUDENT_CLASS_FILE like below, basically a two column table
student1 class_A
student2 class_A
student3 class_B
.....

now if someone gives me a class list, how I could generate the student
lists for each class?

I thought that I could use a code like below, but I don't know exactly
what to write for the third row...

Jie

################# MY PERL CODE#############3


while (STUDENT_CLASS_FILE) {
($student, $class) = split
push @{$STUDENT_CLASS_HASH{$class}} ,$student ????????????
}

foreach $class (@CLASS_LIST) {
$students = join ("," @{$STUDENT_CLASS_HASH{$class}} )
print "$classs, $student\n"
}
 
K

Klaus

I have a STUDENT_CLASS_FILE like below, basically a two column table
student1 class_A
student2 class_A
student3 class_B
....

now if someone gives me a class list, how I could generate the student
lists for each class?

Could be homework, but I'll give you the benefit of the doubt.
I thought that I could use a code like below, but I don't know exactly
what to write for the third row...

....of your perl program ?

Ok, but then you'd probably better say the third *line*.
while (STUDENT_CLASS_FILE) {

What is "STUDENT_CLASS_FILE" ? If it is a file handle, you probably
want to say while (<STUDENT_CLASS_FILE>) instead. And, if I may drop a
suggestion here, it is generally recommended to use lexical file
handles like so:

open my $STUDENT_CLASS_FILE, '<', 'data.txt' or die "Error $!";
while ( said:
($student, $class) = split

you are missing the parameters for split (see perldoc -f split) and
the semicolon at the end of the line.
push @{$STUDENT_CLASS_HASH{$class}} ,$student ????????????

remove the question marks and put a semicolon at the end of the line.
}

foreach $class (@CLASS_LIST) {
$students = join ("," @{$STUDENT_CLASS_HASH{$class}} )

semicolon at the end of the line is
missing.......................................
 
J

Jie

Thank you, Klaus!!

Could be homework, but I'll give you the benefit of the doubt.


...of your perl program ?

Ok, but then you'd probably better say the third *line*.


What is "STUDENT_CLASS_FILE" ? If it is a file handle, you probably
want to say while (<STUDENT_CLASS_FILE>) instead. And, if I may drop a
suggestion here, it is generally recommended to use lexical file
handles like so:

open my $STUDENT_CLASS_FILE, '<', 'data.txt' or die "Error $!";


you are missing the parameters for split (see perldoc -f split) and
the semicolon at the end of the line.


remove the question marks and put a semicolon at the end of the line.



semicolon at the end of the line is
missing.......................................
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top