in reply to How to code a complex AoH?
You've already got some answers to your issue - just note the issue with \@points that I discussed here, including that it's a good idea if you Use strict and warnings.
Since it looks like you're parsing CSV, I'd recommend using a module for this, in particular Text::CSV:
#!/usr/bin/env perl use warnings; use strict; use Text::CSV; my $csv = Text::CSV->new({binary=>1, auto_diag=>2}); $csv->getline(\*DATA); # read and discard header my @candidates; while ( my $row = $csv->getline(\*DATA) ) { my %student; for my $key (qw/ id gender birthday status room seat version /) { # remove the first elements of the arrayref $student{$key} = shift @$row; } # all that's left in the arrayref is the points $student{points} = $row; push @candidates, \%student; } $csv->eof or $csv->error_diag; use Data::Dump; dd \@candidates; __DATA__ 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
Note that DATA is a special filehandle that refers to the __DATA__ section at the end of the script, but you can use any other filehandle here, like FH in your program. Although, it'd be better to switch to "lexical" filehandles, that is open my $fh, '<', $filename or die "open $filename: $!"; (see open).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to code a complex AoH?
by iatros (Novice) on Mar 30, 2017 at 15:42 UTC |