in reply to Re^2: Checking LInes in Text File
in thread Checking LInes in Text File

Provide half a dozen lines of sample data, the test code you are currently using, and a sample of the output you expect to see.

For the test code it is easiest to use a __DATA__ section for the test data rather than an external file and simply print the result rather than generating an external file.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: Checking LInes in Text File
by Anonymous Monk on Jun 02, 2006 at 00:27 UTC
    Thanks GrandFather!

    My data would looks like this:
    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
    so every set of data (MCAT, PCAT, LMAT) is unique except for the fourth and eight which differ only by TYPE.

    I would like to roll the fourth and eight together so it would look like this:

    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

    This leaves me with a total of 7 data groups instead of 8.

    So far my code doesn't even come close to accomplishing doing this....

      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

      DWIM is Perl's answer to Gödel
        Grandfather,

        Thank you very much!!! I really appreciate you solving that problem for me. I'm very new to PERL and have to admit that your code took a while to make sense to me. I didn't realize that you could access an array by the data value, I thought you had to access it by location (0,1,2,3... etc). Thanks again for your help!