PDA

View Full Version : Split an array in multiple pages


mapg
02-21-2005, 04:46 PM
Hi All,

I would like to know if there is an easy way to split this array (a file list in this case) in multiple pages. I have summarized the code to clarify the main function.
##!/usr/bin/perl

print "Content-type: text/html\n\n";

sub get_file_list {
$total_files = 0;

opendir(DIR, ".");
@files = readdir(DIR);
@files = sort { lc($a) cmp lc($b) } @files;
closedir(DIR);

for my $file (@files) {
if (($file eq ".") || ($file eq "..")) {
next;
}
print $file, "<br>";
$total_files++;
}
}

&get_file_list;The limits could be inserted in the QUERY_STRING environment

I appreciate a lot any idea because I really need to split this array because is growing impressively with 1000's files.

Thank you very much in advance.

Mapg

mlseim
02-21-2005, 07:31 PM
This is all I could think of ... splitting the array in half.
Ends up with two arrays.

EDIT (added later):
I got to thinking more about this. Are you interested in splitting
the array, or are you concerned about creating smaller pages, as
in a display on a browser? Maybe you meant that the splitting should
be like 25 files per page (on the browser), not in the array :confused:


Using the "splice" function. Always grab the last part first,
because it lops it off when it's done.

I don't know if this is efficient or not.... and what the limitations
are on array memory size.

But, this might give you some more ideas ....

=================================================

#!/usr/bin/perl

print "Content-type: text/html\n\n";

sub get_file_list {
$total_files = 0;

opendir(DIR, ".");
@files = readdir(DIR);
@files = sort { lc($a) cmp lc($b) } @files;
closedir(DIR);

$number = @files; #find out how many files are in the array.

@part2 = splice(@files, int($number/2),$number); #grab the last half of array.
@part1 = splice(@files, 0, int($number/2)); #grab the first half of array.
@files=(); #clear out the old array.

for my $file (@part1) {
if (($file eq ".") || ($file eq "..")) {
next;
}
print $file, "<br>";
$total_files++;
}
}

&get_file_list;

#############################

mapg
02-21-2005, 07:55 PM
Thank you Mlseim,

Above all, I am interested to split the array to get smaller pages as you intuited.

I am going to implement you code and will reply you.

Thank you again for your help.

Mapg