in reply to Re: Combining flat file records
in thread Combining flat file records
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Combining flat file records
by jZed (Prior) on May 09, 2006 at 17:25 UTC |