[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]data.txt[/purple][red]'[/red][red];[/red]
[gray][i]# Simple array of parsed lines (in order of arrival)[/i][/gray]
[black][b]my[/b][/black] [blue]@data[/blue] = [red]([/red][red])[/red][red];[/red]
[gray][i]# Marking first seen place for each third key[/i][/gray]
[black][b]my[/b][/black] [blue]%first_seen[/blue] = [red]([/red][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [blue]$file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@data[/blue], [red][[/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]'[/red][purple]~[/purple][red]'[/red][red]][/red][red];[/red]
[gray][i]# Format time (assume military time, but not necessarily[/i][/gray]
[gray][i]# with hours as padded 2 digits). This is important in[/i][/gray]
[gray][i]# order to sort using simple string comparison[/i][/gray]
[blue]$data[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red][red][[/red][fuchsia]1[/fuchsia][red]][/red] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]'[/red][purple]%08s[/purple][red]'[/red], [blue]$data[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red][red][[/red][fuchsia]1[/fuchsia][red]][/red][red];[/red]
[blue]$first_seen[/blue][red]{[/red][blue]$data[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red][red][[/red][fuchsia]2[/fuchsia][red]][/red][red]}[/red] ||= [blue]@data[/blue][red];[/red]
[red]}[/red]
[gray][i]# Sort data[/i][/gray]
[blue]@data[/blue] =
[url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red][blue]$first_seen[/blue][red]{[/red][blue]$a[/blue]->[red][[/red][fuchsia]2[/fuchsia][red]][/red][red]}[/red] <=> [blue]$first_seen[/blue][red]{[/red][blue]$b[/blue]->[red][[/red][fuchsia]2[/fuchsia][red]][/red][red]}[/red] || [blue]$b[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] cmp [blue]$a[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red][red]}[/red]
[blue]@data[/blue][red];[/red]
[gray][i]# Filter Unique[/i][/gray]
[black][b]my[/b][/black] [blue]%unique[/blue][red];[/red]
[blue]@data[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]! [blue]$unique[/blue][red]{[/red][blue]$_[/blue]->[red][[/red][fuchsia]2[/fuchsia][red]][/red][red]}[/red]++[red]}[/red] [blue]@data[/blue][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$record[/blue] [red]([/red][blue]@data[/blue][red])[/red] [red]{[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple]~[/purple][red]'[/red], [blue]@$record[/blue][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[fuchsia]1[/fuchsia][red];[/red]
[teal]__END__[/teal]