push into hashs of arrays
am 24.08.2007 20:00:53 von 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"
}
Re: push into hashs of arrays
am 24.08.2007 20:33:26 von Klaus
On Aug 24, 8:00 pm, Jie wrote:
> 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 () 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 (<$STUDENT_CLASS_FILE>) {
> ($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.......................................
> print "$classs, $student\n"
> }
--
Klaus
Re: push into hashs of arrays
am 27.08.2007 17:11:31 von Jie
Thank you, Klaus!!
On Aug 24, 1:33 pm, Klaus wrote:
> On Aug 24, 8:00 pm, Jie wrote:
>
> > 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 () 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 (<$STUDENT_CLASS_FILE>) {
>
> > ($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.......................................
>
> > print "$classs, $student\n"
> > }
>
> --
> Klaus