Like most other posters, I'm new to perl and have been learning it for the past week. Due to the nature of the assignment, I'm unfortunately unable to post any code, and the limitation I'm under requires that variables be declared at the top of the program, c-style. I've been assigned to a company project to create a tool that reads a text file, goes through every line and removes duplicates and non-essential lines, then performs certain actions on the unique lines. I managed to get a working, but inefficient script written. The problem I ran into was how to efficiently compare lines of text in a text file that is at least 60mb. I managed to cut the run time from 3 hours to 15 minutes by restricting how often the script checks for duplicates, but that's still too long.

The solution I came up with is rather than comparing each read line with a list of existing unique lines, break up the lines into 3 keywords, then search a data tree for matches. I'll get to the tree's structure later in the post.

To understand the structure of the tree, each line that needs to be compared uses the following format:
"..\..\folder\folder\file.ext", line #: junk: rule #:
I need to compare the file name, line #, and rule # to the corresponding information from previous lines. If all three keywords match, the line is a duplicate and can be tossed aside. If not, the line needs to be added to the data tree for the next loop iteration. Getting these values hasn't been a problem.

A picture would be best, but I don't have the means to upload one, so here's my best shot. The format of the data structure, using perl's limitations, might be that of an array with length 3, where the first entry contains a list of Rule #s (Rule #s are the fewest in number, so are searched first). The second entry is a 2D matrix containing a list of files under each rule. The third entry would be a 3D matrix containing a list of line #s for every file.

Which leads to my problem. I've read up on creating multi-dimensional arrays in perl. What I can't find are any examples of a data tree that isn't binary. My biggest question is how do I declare the array(s)? Is it something like:

my @tree = {@R[],@F[@F2[]],@L[@L1[@L2[]]]};

I'll need to use some nested loops to search and add to the tree, so how do I iterate through the correct index of the correct array?

Also, should I use hashes instead of arrays? Is there an efficient way to do string comparisons (the comparison itself), therefore skipping the whole data tree mess?

In reply to Self-Populating Tree Data Structure by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.