in reply to Combining flat file records

Perhaps this is fishing for you rather than teaching you to fish, but here's how I'd do it:
#!/usr/bin/perl -w use strict; my %result; for my $row(<DATA>) { my($num,$date,$time)=split /\s+/,$row; push @{ $result{$num}->{$date} }, $time; } for my $num(sort keys %result) { for my $date(sort keys %{$result{$num}}) { my %contains; my @time_ary; for my $time(sort @{$result{$num}->{$date}} ){ push @time_ary, $time unless $contains{$time}++; } printf "%s\t%s\t%s\n", $num,$date,join(',',@time_ary); } } __DATA__ 848 05/23/06 11:00 848 05/23/06 12:30 848 05/23/06 13:00 848 05/23/06 14:00 848 05/25/06 11:00 848 05/25/06 12:00 261 05/24/06 11:00 261 05/24/06 12:30 261 05/24/06 13:00 261 05/24/06 13:00 261 05/24/06 13:00 261 05/24/06 13:00

Replies are listed 'Best First'.
Re^2: Combining flat file records
by ikegami (Patriarch) on May 08, 2006 at 23:16 UTC

    If speed or memory is an issue, one could avoid loading the entire file into memory as follows:

    my $last_id; my $last_date; my @times; for (;;) { my $line = <DATA>; my ($id, $date, $time) = split(' ', $line||''); if ($. != 1) { if ((!defined($id) && @times) || $id ne $last_id || $date ne $last_date ) { print(join("\t", $last_id, $last_date, join(',', @times)), "\ +n"); $#times = -1; } } last unless defined($line); $last_id = $id; $last_date = $date; push(@times, $time) if !@times || $times[-1] ne $time; } __DATA__ 848 05/23/06 11:00 848 05/23/06 12:30 848 05/23/06 13:00 848 05/23/06 14:00 848 05/25/06 11:00 848 05/25/06 12:00 261 05/24/06 11:00 261 05/24/06 12:30 261 05/24/06 13:00 261 05/24/06 13:00 261 05/24/06 13:00 261 05/24/06 13:00

    It also preserves the order of the ids in the output.

      Um, yeah. I was being lazy. Better to get it all in one pass as you show.
Re^2: Combining flat file records
by pglinx (Acolyte) on May 08, 2006 at 23:34 UTC
    Thank you, I new how to loop through a file using foreach to reformat the output, but was not sure how to actually combine it, especially in one field. Thanks for the help and for teaching me how to fish. It worked great.