Just for completeness an approach for the unsorted case.¹

/foo/bar/baz/123 aaa /foo/bar/baz/123 aab /foo/bar/baz/123 aac /foo/bar/baz/124 aaa /foo/bar/baz/124 aab

You could still be able to profit from automated swapping of unused data-structures by using more hierarchies of hashes and splitting the paths at '/'.

For instance $duplicates{foo}{bar}{baz}{123}++ would only involve some comparatively small hashes kept in memory.

The drawback would be that swapping to HD costs time! That's why you should try to minimize swapping by concentrating on only some hashes currently loaded.

That means you would need multiple runs scanning your file, while you concentrate on a part of "unfinished" hashes (e.g. all path starting with /foo/bar and so on)

I think always loading 1 million lines into memory (i.e. an array) and scanning them in multiple runs could lead to a balanced mix of time and memory complexity.

And already scanned elements could be marked by delete $arr[$index]

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

¹) I just realised that using multiple hash-levels elegantly solves the case of sorted input. You won't need multiple runs, the sorted input will effectively minimize the amount of swapped hashes.


In reply to Re: Large file, multi dimensional hash - out of memory (swapping) by LanX
in thread Large file, multi dimensional hash - out of memory 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.