push into hashs of arrays - Perl

This is a discussion on push into hashs of arrays - Perl ; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

push into hashs of arrays

  1. Default push into hashs of arrays

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


  2. Default Re: push into hashs of arrays

    On Aug 24, 8:00 pm, Jie <jiehuang...> 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 (<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 (<$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


  3. Default Re: push into hashs of arrays


    Thank you, Klaus!!

    On Aug 24, 1:33 pm, Klaus <klau...> wrote:
    > On Aug 24, 8:00 pm, Jie <jiehuang...> 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 (<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 (<$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




+ Reply to Thread

Similar Threads

  1. Two arrays: return only values that exist in both arrays?
    By Application Development in forum RUBY
    Replies: 8
    Last Post: 11-23-2007, 02:04 PM
  2. Push server
    By Application Development in forum Inetserver
    Replies: 5
    Last Post: 09-28-2007, 07:23 AM
  3. J2ME program for Push Email, IM, Push RSS on your mobile phone!
    By Application Development in forum Java
    Replies: 0
    Last Post: 07-19-2007, 10:00 AM
  4. Best way to push XML to a web app
    By Application Development in forum XML SOAP
    Replies: 5
    Last Post: 09-05-2006, 02:18 PM
  5. marshal 2-d arrays, structure members, and arrays of structures
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 04-11-2006, 11:59 AM