in reply to Re: (jeffa) Re: associative array problem
in thread associative array problem

Oh don't worry - i ran the code serveral times. Again . What Is It Suppose To Do!!!!!

Sigh, at any rate, here is something for you to mess with:

use strict; use Data::Dumper; use CGI qw(header params); # this prints the content-type for you # you can use params() to get form values without # having to roll your own code print header(); my %INPUT = ( quizes => 2, assign => 2, recit => 2, ndays => 1, AT32000011 => 1, QZ132000011 => 1, RI132000011 => 1, AS132000011 => 1, QZ232000011 => 2, RI232000011 => 2, AS232000011 => 2, AT32000012 => 1, QZ132000012 => 12, RI132000012 => 12, AS132000012 => 12, QZ232000012 => 22, RI232000012 => 22, AS232000012 => 22, ); my %STUDQT; foreach (32000011..32000012) { push @{$STUDQT{$_}}, qw(fname mname lname Sex); } my @sorted = sort { "$STUDQT{$a}[3] $STUDQT{$a}[2] $STUDQT{$a}[0]" cmp "$STUDQT{$b}[3] $STUDQT{$b}[2] $STUDQT{$b}[0]" } keys %STUDQT; #Get The Inputs From User # remember, CGI can do this for you - see params() foreach my $l (@sorted) { my $STUD_1 = $STUDQT{$l}[1]; for my $i (1..$INPUT{'assign'} + 1) { if ($INPUT{"AS$i$l"}) { $STUD_1 = "$STUD_1$INPUT{\"AS$i$l\"}"; $STUD_1 .= "~" unless $INPUT{'assign'} == $i; } } $STUDQT{$l}[1] = "$STUD_1"."^"; print "key('32000011') is = $STUDQT{'32000011'}[1] <br>\n"; print "key{'32000012'} is = $STUDQT{'32000012'}[1] <br>\n"; }
Basically, i removed all instances of ['02'] ... i don't think that these do what you think that they do. Add them back and watch strict complain. Now, if i _knew_ what the GOAL was (hint hint) - i could give a better answer. But i am not going to play 'Guess my problem' any longer with this code.

jeffa

Replies are listed 'Best First'.
Re: (jeffa) 3Re: associative array problem
by Gerryjun (Scribe) on Nov 02, 2001 at 22:41 UTC
    Sorry! ok this is why my hash looks like that, i have a student record, each student has a student ID which is my Key, now per student i have 12 subjects & per subject 5 entries attendance, quiz, ricitation, assignment...

    Ex.
    $TT{Student_ID}[subject][entries]


    please advise on proper format...
      Ok - what you have _should_ work, but there is a better solution. You should treat entries just like you treat subject - when you surround the entry index with single quotes, i assumed you really wanted a hash.

      Sounds like you need to consider using a Database for this. You would have a table for students, their ID will be the primary key. You will need another table that lists the available subjects, with some unique ID for each one of those. You will need a cross-reference table to join Students with there subjects. It will consist of two fields, one for the Student ID and one for the Subject ID.

      From there you will need two similar tables for the entries that links them to the proper subject.

      Not using a database on a complex problem like this tends to make your code 700+ lines; using a database on a problem like this tends to make your code MUCH smaller:

      select students.lname, students.fname, subjects.name, entries.name from students left join subjects on students.id = subjects.student_id left join entries on subjects.id = entries.subject_id where students.id = ? order by students.Sex, students.lname, students.fname
      A wham! You now have all subjects and entries for student '?' - and they are ordered by the database, not Perl.

      Databases by themselves are beyond the scope of this site, but be sure and read up on DBI.

      jeffa

        It should work then my hash is ok? but i still cant find why a hash with different key also gets the value of the original key i wanted to access. Ex.

        $TT{1000001}[1][1] = '4342'; $TT{1000002}[1][1] = also gets the value!


        Im about to finish this so i cant just start from scrap again & co'z i dont know how to use DBI yet, i have been coding on Perl for only 2 months.

        Please stir me to what part of my code is the problem! and also please advice on how to best make do to what im currently using!

        And a note was posted on ChatterBox that in perl "there is No such thing as associative array but their is hash that is Similar to it in Perl", i thought that, hash and associative arrays where both the same! if so "Perl for Dummies" is really Dumb? it named that function as associative array! thanks!