Reputation: 37
Here is a task that's puzzling me for quite a while now: Regularly, we get exam results of our students as a .csv file. The header has some meta data such as ID, gender, date of birth, status, exam room, seat number, exam version, and the score for 60 questions (0.0 0.5 1.0 points if the answer is wrong, half-correct, or correct). There are 6 versions (A - F) of the exam differing only by the order of the 60 questions. The information is stored for statistical evaluation which requires the correct alignment according to the exam master (a .txt file with 7 columns for version A-F and the correct answer in the 7th column). I tried to accommodate the .csv file as an array of hashes to generate a different .csv or tabbed .txt file in which all exam results appear in a unified order for later statistical evaluation.
Example: header -- ID,gender,birthdate,order,room,seat,version,points,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
277710814533,f,01/02/1993,m,sr_3,A11,A, 1,1,1,1,0,1,1,1,.5,1,1,1,0,1,.5,1,1,1,0,1,.5,1,1,0,1,1,1,1,1,1,1,0,0,1,0,1,.5,1,1,1,1,.5,0,1,1,1,0,1,1,1,1,1,0,1,1,1,.5,1,1,1
755310765962,f,31/07/1992 00:00,v,aula,C11,C,1,.5,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,.5,1,0,.5,1,0,1,.5,0,.5,0,1,0,0,.5,1,1,0,.5,1,1,.5,.5,1,.5,.5,1,1,1,.5,.5
394610513538,m,20/10/1992 00:00,m,sr_3,E13,E,1,1,0,.5,1,1,1,1,1,1,1,.5,1,1,.5,.5,1,1,1,.5,.5,1,1,1,1,0,0,.5,1,1,.5,.5,.5,.5,0,1,0,.5,0,0,1,0,1,.5,0,1,0,0,.5,1,0,1,1,0,.5,.5,.5,.5,.5,.5
The code generates hash keys according to the following scheme:
while ( <FH> ) {
chomp ;
if ( /^\d\d\d/) {
( $id , $gender , $birthday , $status , $room , $seat , $version , @points ) = split ( /,/ , $_ ) ;
$student = {
'id' => $id ,
'gender' => $gender ,
'birthday' => $birthday ,
'position' => $position ,
'room' => $room ,
'seat' => $seat ,
'version' => $version ,
'points' => @points
} ;
push ( @candidates , $student ) ;
}
} ;
close FH ;
print "Number of candidates processed: " . ( $#candidates + 1 ) . "\n" ;
The compiler throws a warning for each record, e.g. "Odd number of elements in anonymous hash at /Documents//testAoH.pl line 38, line 16." but the script is executed.
The script prints the correct number of processed records, but when I try to retrieve a specific record I only get the scalar values and the @points array yields only one (the first?) result as if it were destroyed. A data dumper output further shows that something must be internally wrong with this code.
Data Dumper e.g.
755310765962
$VAR1 = \{
'0' => '0',
'gender' => 'f',
'id' => '755310765962',
'points' => '1',
'room' => 'aula',
'.5' => undef,
'1' => '.5',
'birthday' => '31/07/1992',
'seat' => 'A11',
'version' => 'A',
'status' => 'v'
};
Any clues?
Upvotes: 1
Views: 46
Reputation: 2288
Use \@points. @points expands in the hash constructor to generate: 'Points' => $points[0], $points[1] => $points[2], ...
Upvotes: 2