sub mdrunner { my $passed_dir = $_[0]; my $dirtoget="${passed_dir}/StretchingDecaAlanine/GMXCubicBox/Umbrella"; my (@directories, @part) = (); opendir(IMD, $dirtoget) || die("Cannot open directory"); my @files= readdir(IMD); closedir(IMD); foreach my $g (@files) { push(@directories, $g) if ((-d $g) and ($g ne ".") and ($g ne "..")); } while(scalar(@directories) > 0) { #Take first 4 when there are 4 or more elements in the array if(scalar(@directories)>=4){ @part= @directories[0..3]; } #Take as many as possible when <4 elements in the array elsif(scalar(@directories)<4){ @part = @directories [0..scalar(@directories)]; } #Shorten the array by the number of elements taken in @part for(my $i=0; $ilogje 2>&1 &' or die; } chdir ("${passed_dir}/StretchingDecaAlanine/GMXCubicBox/Umbrella"); } while (wait() != -1) {} print "Done with mdrun in"."@part\n"; } }