use strict; use warnings; use Data::Dumper; my @results; while () { my @row = split; for (my $idx = 0; $idx < 3; $idx++) { push @{ $results[$idx] }, $row[$idx]; } } # Assume @results already populated... my ($day_of_week, $production_line, $output) = @results; my @data_to_plot; # Ignore the headers for (my $idx = 0; $idx < 3; $idx++) { shift @{ $results[$idx] } } ### # Extending an idea from [http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q5.4.html]... my %saw; my %production_lines = ( map { $_ => [] } grep ! $saw{$_}++, @$production_line ); # ### for (my $index = 0; $index < @$day_of_week; $index++) { push @{ $data_to_plot[0] }, $day_of_week->[$index] unless grep /^$day_of_week->[$index]$/, @{ $data_to_plot[0] }; push @{ $production_lines{$production_line->[$index]} }, $output->[$index]; } push @data_to_plot, ( map { $production_lines{$_} } sort keys %production_lines ); print Dumper \@data_to_plot; __DATA__ DayOfWeek ProductionLine Output 2 CANNING 18353 2 MULTIPACK 14878 2 QUEST 911 3 CANNING 46775 3 MULTIPACK 42601 3 QUEST 1564 4 CANNING 81302 4 MULTIPACK 67542 4 QUEST 1879 . .