Greetings Perl Monks! Using standard Perl on CentOS 5.5. I have an array that I create in main, a list of file names. The array is just a list of items to process. My problem is that after I fork (max of five forks), I try to set the $file variable to the result of shift (@fileArray). It does get the variable, but it's always the first element, which means it's not shifting. Is there something I have to do to make it shift?
#!/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; }
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:
my $file = shift @fileArray; my $file = shift(@fileArray);
Thanks to everyone for the help! -AK

In reply to assign variable and shift array by AK7033

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.