There's a few problems. The reason it is going into an infinite loop: there is nothing incrementing the $next_index variable. You increment while finding the first file small enough, but then never do again.

Another issue is that by the time you try to use the @files array, it is an array of file sizes, not file names. Fine to find a group of file sizes under a limit, but no way to actually access the file names to add them to the zip file.

Thirdly, you are pushing elements onto the end of an array, then using the first array element in calculations. You probably should use the element just pushed instead.

Update: Oh yeah, the splice length needs to be non zero also, or it won't modify the array.

Putzing around a bit... (note: I do not particularly recommend this, just fixed to be a working example. No guarantees about correct logic.)

#!/usr/bin/perl -w use strict; use File::Find; use File::Basename; use File::stat; use File::Slurp qw(read_dir); my @directories = qw( /all_files_a /all_files_b); my @files; my $min_size = 1024; # bytes => 1K my $max_size = 10485760; # bytes =>10MB for my $d (@directories) { push @files, { name => "d/$_", size => -s $_ } for grep { -f && - +s _ >= $min_size && -s _ <= $max_size && /\.pdf/} read_dir( $d, prefi +x => 1 ); } my $limit = 5_000_000; #Create zip with $limit of files per zip print "Creating zips...\n"; my $zip_number = 1; my $total_size = 0; my @other_group; my $next_index = 0; while(@files) { #Find biggest file that will fit $next_index++ while( ($next_index <= $#files) && ($total_size + $f +iles[$next_index]->{size} > $limit) ); #If there was a file that will fit, add it to @other_group if($next_index <= $#files) { push @other_group, splice(@files, $next_index, 1); $total_size += $other_group[-1]->{size}; $next_index++; } #Otherwise, zip this group, and start a new group else { # print this for testing, these files will be zipped in the $z +ip_number.zip file foreach my $testfiles (@other_group) { print $testfiles->{name}," - $zip_number.zip\n"; } #Clear this group info, and increment zip number @other_group = (); $total_size = 0; $zip_number++; $next_index = 0; } }

In reply to Re: Grouping files before zipping! by thundergnat
in thread Grouping files before zipping! 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.