in reply to Re^4: Checking LInes in Text File
in thread Checking LInes in Text File
The following is ok for reasonable size files but may bog down when things get huge.
use strict; use warnings; use Data::Dump::Streamer; my %firstLines; my @lines; while (<DATA>) { chomp; my ($data, $type) = /(.*)\s+TYPE:\s+(\w+)$/; next if ! defined $type; # ignore malformed line if (exists $firstLines{$data}) { $lines[$firstLines{$data}] .= ", $type"; } else { $firstLines{$data} = @lines; push @lines, $_; } } print join "\n", @lines; __DATA__ MCAT: 0xf30cbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc3fbed1 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xeeccbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1 MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA0 MCAT: 0xeeecbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc000331 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xe554be01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1 MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA1
Prints:
MCAT: 0xf30cbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc3fbed1 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xeeccbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1 MCAT: 0xf30cbe91 PCAT: 0xafaddd09 LMAT: 0x00040000 TYPE: KA0, KA1 MCAT: 0xeeecbe01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA0 MCAT: 0xcc000331 PCAT: 0x000fb109 LMAT: 0x00000800 TYPE: KA1 MCAT: 0xe554be01 PCAT: 0xcda2b409 LMAT: 0x00100000 TYPE: KA1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Checking LInes in Text File
by Anonymous Monk on Jun 02, 2006 at 16:52 UTC | |
by GrandFather (Saint) on Jun 02, 2006 at 23:38 UTC | |
by ibeneedinghelp (Initiate) on Jun 05, 2006 at 23:11 UTC |