It would help if you could provide an example of exactly what the data looks like since it is too easy to misunderstand prose sometimes.

One thing to say is that if this is any sane kind of config file, i.e. way smaller than Sendmail's, then who really cares about efficiency.

Next, if you mean the config data is something like a hash of hashes, of hashes, of hashes then obviously you can find your data like $val = $cfg{section}{subsection}{param}{option} and that should be very efficient.

Now if you are wanting to search the entire HoHoHoH for certain values, the most efficient way is probably going to be to make an inverted index. For example you could make a hash that looks like $inv{"$val_$instance"}="$section_$subsection_$param_$option" where $instance is just a counter which lets you maintain records for multiple occurrences of the same string in different parts of the config file without overwriting old hash entries.

The point of the inverted index is that you trade a little time at the beginning for the ease of being able to just read off the value at the end. Probably you can even use grep with a regular expression on the keys of the inverted index. Come to think of it, you could just run a pattern match on the config file itself and the heck with all this hash stuff no? Hard to see your exact requirement.

About implementing a real search algorithm, it would probably make more sense if you are talking about a lot of data and you also know a lot about the kind of data you have and where it will be in the structure.

Some other neat links I was finding in CPAN.. Search-InvertedIndex, Tie::Hash::Regex (mentions PM!), Data::Hash::Flatten (also mentions PM, and your problem!). And last but not least, Data-DRef (thanks to princepawn's post in the Flatten thread) which looks useful in the "matching_keys" and "get_value_for_key" methods (caveat emptor, haven't used it myself). Dref stuff looks neat and flattens it all for you.

Hope this helps!


In reply to Re: searching complex data structures by mattr
in thread searching complex data structures 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.