AK7033 has asked for the wisdom of the Perl Monks concerning the following question:
I can get the first element of the array, but the shift appears to have no affect at all. All five forks have the same filename, so I suppose I've done something wrong here. Everything works aside from the file name, which does not change from How can I remove that element from the array once I have assinged the value to my $file variable? I can't get either of these two to work:#!/usr/bin/perl 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++) { $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"; my $file = shift(@fileArray); print "DEBUG: $file \n"; procFiles($forkCount, $$, $file); exit 0; } else { die "couldnt fork: $!\n"; } } ## we will stand by here and wait for each of those pids to complete ## The log entry for each fork will go here. foreach (@childArray) { my $procId = waitpid($_, 0); print "LOG: process number $procId\n"; } print "Main Terminated\n"; sub procFiles { ## simulate delay in processing dissimilar files srand(); my $minDelay = 1; my $maxDelay = 2; my $timeDelay = int(rand($maxDelay)) + $minDelay; ## get the fork number, process id and filename my $forkCount = $_[0]; my $procId = $_[1]; my $fileName = $_[2]; ## get the process id for this fork my $processIDnum = shift; ## sleep $num; print "Completed child process ($procId), fork number $forkCount, f +ile name $fileName\n"; ## retun the process id, which will be picked up return $processIDnum; }
Thanks to everyone for the help! -AKmy $file = shift @fileArray; my $file = shift(@fileArray);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: assign variable and shift array
by fidesachates (Monk) on May 06, 2011 at 19:00 UTC | |
by ikegami (Patriarch) on May 06, 2011 at 20:59 UTC | |
by AK7033 (Initiate) on May 06, 2011 at 20:44 UTC | |
|
Re: assign variable and shift array
by NetWallah (Canon) on May 06, 2011 at 18:55 UTC | |
by AK7033 (Initiate) on May 06, 2011 at 19:10 UTC | |
|
Re: assign variable and shift array
by ikegami (Patriarch) on May 06, 2011 at 21:02 UTC |