in reply to Weighted averages
Hello drose2211,
For my point of view why not to use HASHES OF HASHES? I think is the best solution for your problem.
Sample of code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoH = ( student1 => { quiz => "result", exam => "result", final => "result", }, student2 => { quiz => "result", exam => "result", final => "result", }, student3 => { quiz => "result", exam => "result", final => "result", }, ); print Dumper \%HoH; __END__ $ perl test.pl $VAR1 = { 'student3' => { 'quiz' => 'result', 'final' => 'result', 'exam' => 'result' }, 'student1' => { 'final' => 'result', 'quiz' => 'result', 'exam' => 'result' }, 'student2' => { 'exam' => 'result', 'quiz' => 'result', 'final' => 'result' } };
Regarding on how to process the data and store them read the doc on the link above, alternatively provide (a few lines) as a sample of your CSV file and we can help you.
Update: There are many many ways of resolving your problem. For a quick proposed solution something like that could be done using HASHES OF ARRAYS and HASHES OF HASHES. You can make the code sorter and more efficient with minor modifications but this should be enough to get you started.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::Util qw(sum); sub quizSub { return (sum(@_) / 300) * .10; } sub examSub { return (sum(@_) / 200) * .20; } sub finalSub { return (sum(@_) / 100) * .30; } my %HoA; while (<>) { chomp; my @data = split(/,/, ); my $name = shift @data; $HoA{$name} = [ @data ]; } continue { close ARGV if eof; } # print Dumper \%HoA; my %HoH; # print the whole thing sorted by number of members and name foreach my $key ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} || $a cmp $b } keys %HoA ) { # print "$key: ", join(", ", sort @{ $HoA{$key} }), "\n"; my $quiz1 = shift @{ $HoA{$key} }; my $quiz2 = shift @{ $HoA{$key} }; my $quiz = quizSub($quiz1, $quiz2); $HoH{$key}{'quiz'} = $quiz; my $exam1 = shift @{ $HoA{$key} }; my $exam2 = shift @{ $HoA{$key} }; my $exam = examSub($exam1, $exam2); $HoH{$key}{'exam'} = $exam; my $final1 = shift @{ $HoA{$key} }; my $final2 = shift @{ $HoA{$key} }; my $final = finalSub($final1, $final2); $HoH{$key}{'final'} = $final; } print Dumper \%HoH; __END__ $ perl test.pl in.txt $VAR1 = { 'Student2' => { 'quiz' => '0.001', 'final' => '0.033', 'exam' => '0.007' }, 'Student3' => { 'final' => '0.033', 'exam' => '0.007', 'quiz' => '0.001' }, 'Student1' => { 'final' => '0.033', 'exam' => '0.007', 'quiz' => '0.001' } };
Sample of input data, based on the sample that you provided it:
$ cat in.txt Student1,1,2,3,4,5,6 Student2,1,2,3,4,5,6 Student3,1,2,3,4,5,6
Hope this helps, BR.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Weighted averages
by drose2211 (Sexton) on Jan 25, 2018 at 16:35 UTC | |
by thanos1983 (Parson) on Jan 25, 2018 at 17:04 UTC | |
by drose2211 (Sexton) on Jan 25, 2018 at 17:54 UTC | |
by thanos1983 (Parson) on Jan 25, 2018 at 18:05 UTC |