in reply to merge multiple files giving out of memory error
You only seem to be using the number of values and their sum, so you don't have to keep all the values in your arrays. Thanks to the magic of autovification, you can just write:
# at the top of your program use constant { Key => 0, Sum => 1, Count => 2 }; # How you fill the %seen hash $seen{$key1}[Key] //= $key; ### Edit, turned = $key into //= $key $seen{$key1}[Sum] += $value; # Cumulated value of $key1 $seen{$key1}[Count] ++; # Number of time $key1 has been seen
If that's still not enough, you can export the memory consumption elsewhere (eg: on your hard drive) by using a database tied to your hash; DBM::Deep seems like a good candidate for that, although I have never used it. This won't make your program any faster though.
THe code is working perfectlyWell now that's a mystery, because while (<>) shifts (removes) the file names from @ARGV before opening the files, so @ARGV has to be empty by the time you try to get the file count. my $file_count = @ARGV; should be at the top of the file (and can be done only once). By the way, your array contains the key and all the values, so even if you have just one value, the array would be of size 2.
About the hanging part, that's to be expected when there's a lot of data to process. You can add a message to tell you how far the processing has gone (and know if it is actually frozen or just not done yet). print "Done processing $ARGV\n" if eof; (at the end of the first loop) will print a message each time the end of a file is reached (see eof).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: merge multiple files giving out of memory error
by Anonymous Monk on Feb 27, 2017 at 11:34 UTC | |
by Eily (Monsignor) on Feb 27, 2017 at 12:31 UTC | |
by Anonymous Monk on Mar 02, 2017 at 09:01 UTC | |
by Eily (Monsignor) on Mar 02, 2017 at 09:17 UTC |