in reply to assign variable and shift array

AK7033, First you need an explanation. You're forking which means all variables are copied into their separate spaces. This means that when you shift the array, each forked process shifts their own copy of the array. That is why you think the array doesn't shrink. To correctly get the different file names in the array, I've used a variable called $arrayIterator which changes every time you fork. This will allow you to access the index in the array for that forked process. Happy coding!

use strict; use warnings; print "Begin Main\n"; ## we store the child process in this array my @childArray; ## path to the files go in this array. It could be one or a dozen ## and is determined by another process our @fileArray = qw(fileone.txt filetwo.txt filethree.txt); ## This is the number of forks we will allow my $forkCeiling = "5"; my $pid; my $fileName; for ( my $forkCount = 1; $forkCount <= $forkCeiling; $forkCount++) { my $arrayIterator = $forkCount-1; $pid = fork(); if ($pid) { push(@childArray, $pid); } elsif ($pid == 0) { # call the download subroutine and pass the number of ++forks, parent pid and array print "Sending fork number $forkCount to the \"process + files\" subroutine...\n"; print $arrayIterator."\n"; my $file = $fileArray[$arrayIterator]; if(defined $file) { print "DEBUG: $file \n"; #procFiles($forkCount, $$, $file); } exit 0; } }

Replies are listed 'Best First'.
Re^2: assign variable and shift array
by ikegami (Patriarch) on May 06, 2011 at 20:59 UTC
    Alternatively, shift before forking.
    my $file = shift(@fileArray); my $pid = fork(); ...
Re^2: assign variable and shift array
by AK7033 (Initiate) on May 06, 2011 at 20:44 UTC
    That fixed the problem. Thanks very much for your help, I've been on this problem for a couple of days. Have a good weekend, -AK