in reply to which data structure do I need for this grouping problem?

Hi, I'd suggest you build a hash keyed by name and then a sub-hash per name keyed by the date, with the values stored in an array. (To lovers of acronyms this would be a HOHOA (hash of hashes of arrays)).

use strict; use warnings; use feature 'say'; my %result; for my $line (<DATA>) { chomp $line; my ( $name, $date, $val ) = split ' ', $line; push @{ $result{ $name }{ $date } }, $val; } for my $name ( keys %result ) { say $name; for my $date ( keys %{ $result{ $name } } ) { say "\t$date: @{ $result{ $name }{ $date } }"; } } __DATA__ nick 20/5/1950 one john 18/2/1980 two nick 19/6/1978 three nick 20/5/1950 four nick 12/9/2000 five john 15/6/1997 six nick 20/5/1950 seven
Output:
$ perl 1221691.pl john 15/6/1997: six 18/2/1980: two nick 19/6/1978: three 20/5/1950: one four seven 12/9/2000: five

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: which data structure do I need for this grouping problem?
by bliako (Abbot) on Sep 04, 2018 at 20:15 UTC

    and that would be a HoTHel !

    edit: he said tab-separated so '\t' probably?