In case you are thinking it might be in the calling code, I used it exactly as the file handle example.

Not so friendly. I knocked together the following from your two posts:

#! perl -slw use strict; use List::Util 'first'; sub gen_merger { my( $list, $fetch, $compare, $finish ) = @_; my @item = map $fetch->($_), @$list; my $done; return sub { return $finish if $done; my $idx = first{ $item[ $_ ] ne $finish } 0 .. $#item; my $next = $item[ $idx ]; for( $idx + 1 .. $#item ) { next if $item[ $_ ] eq $finish; my $result = $compare->( $next, $item[$_] ); #$next = $item[$_] if $result == 1; # Need to keep track of which one we use not just value ( $idx, $next ) = ( $_, $item[$_] ) if $result == 1; } $item[ $idx ] = $fetch->( $list->[ $idx ] ); #$done = 1 if ! first {$item[$_] ne $finish} $idx .. $#item; # First element of array is 0 so use defined instead of truth $done = 1 if ! defined first {$item[$_] ne $finish} $idx .. $# +item; return $next; }; } my $finish = 'A val that is guaranteed not to be present in any list +'; my @list; open $list[ $_-1 ], '<', "file$_" or die $! for 1 .. 9; my $fetch = sub { my $fh = shift @_; return $finish if eof $fh; return scalar <$fh>; }; my $compare = sub { my( $line1, $line2) = @_; my( $stamp1 ) = $line1 =~ /^(\d+)/; my( $stamp2 ) = $line2 =~ /^(\d+)/; return $stamp1 <=> $stamp2; }; my $next = gen_merger( \@list, $fetch, $compare, $finish ); while( 1 ) { my $item = $next->(); last if defined $item && $item eq $finish; printf "$item"; }

Gen'd 9 sorted files of 100,000 integers each, and merged them:  C:\test\890799>..\890799 >merged. The merge completed successfully and correctly and used a rock steady 3.1MB from start to finish.

There is something in your calling code that is different from your old post.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Help Diagnosing Memory Leak by BrowserUk
in thread Help Diagnosing Memory Leak by Limbic~Region

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.