use strict; use warnings; use Data::Dumper; use MCE::Loop; my $ProbTrajfile="states_example.csv"; my $ProbTrajtable_file="states_example_table.csv"; open(my $ProbTraj,"<",$ProbTrajfile) or die; my $line=<$ProbTraj>; my @firstlinelist=split(/\t/,$line); my $name; my $IndexProba=4; my @firstline_woutH=@firstlinelist[4..$#firstlinelist]; foreach $name (@firstline_woutH) { $_=$name; if (! m/HD=/) { last;} $IndexProba++; } our $IndexLine=0; $line=<$ProbTraj>; chomp($line); my @linelist=split(/\t/,$line); our %FullGlobal; $FullGlobal{Time}[$IndexLine]=$linelist[0]; $FullGlobal{TH}[$IndexLine]=$linelist[1]; $FullGlobal{ErrTH}[$IndexLine]=$linelist[2]; $FullGlobal{H}[$IndexLine]=$linelist[3]; my %ProbState; my %ErrProbState; #create probState hash for (my $i=$IndexProba;$i<=$#linelist;$i=$i+3) { my $State="[".$linelist[$i]."]"; $ProbState{$State}[$IndexLine]=$linelist[$i+1]; $ErrProbState{$State}[$IndexLine]=$linelist[$i+2]; } MCE::Loop::init { use_slurpio => 1, chunk_size => '20k', max_workers => 16, init_relay=>0, #gather => MCE::Candy::out_iter_fh($ofh), }; %FullGlobal=mce_loop_f{ my ( $output, $mce, $chunk_ref, $chunk_id ) = ( '', @_ ); open my $ifh, "<", $chunk_ref; while(<$ifh>) { $IndexLine++; print $IndexLine, $/; foreach my $State (keys(%ProbState)) { $ProbState{$State}[$IndexLine]=0; $ErrProbState{$State}[$IndexLine]=0; } chomp($_); @linelist=split(/\t/,$_); $FullGlobal{Time}[$IndexLine]=$linelist[0]; $FullGlobal{TH}[$IndexLine]=$linelist[1]; $FullGlobal{ErrTH}[$IndexLine]=$linelist[2]; $FullGlobal{H}[$IndexLine]=$linelist[3]; for (my $i=$IndexProba;$i<=$#linelist;$i=$i+3) { my $State="[".$linelist[$i]."]"; if (defined $ProbState{$State}[$IndexLine-1]) { $ProbState{$State}[$IndexLine]=$linelist[$i+1]; $ErrProbState{$State}[$IndexLine]=$linelist[$i+2]; } else { for(my $PastIndexLine=0;$PastIndexLine<$IndexLine;$PastIndexLine++) { $ProbState{$State}[$PastIndexLine]=0; $ErrProbState{$State}[$PastIndexLine]=0; } $ProbState{$State}[$IndexLine]=$linelist[$i+1]; $ErrProbState{$State}[$IndexLine]=$linelist[$i+2]; #print "$IndexLine\t",$ProbState{$State}[$IndexLine]=$linelist[$i+1],"\n"; } } } close $ifh; MCE->gather(%FullGlobal); # MCE->gather( $chunk_id, $output, \$IndexLine ); #gather => MCE::Candy::out_iter_array(\$IndexLine), # return $IndexLine; # MCE::relay { # MCE->gather(\$IndexLine) # }; # MCE->relay_unlock( # sub { # $_=$IndexLine; # }); #print Dumper(\%FullGlobal); } $ProbTrajfile; #write the trajectory table in file (the hashes ain't accessible outside mce_loop_f my @StateList=keys(%ProbState); open (my $ProbTrajTable,">", $ProbTrajtable_file) or die ($!); print $ProbTrajTable "Time\t"; print $ProbTrajTable "TH\t"; print $ProbTrajTable "ErrTH\t"; print $ProbTrajTable "H\t" ; foreach my $State (@StateList) { $_=$State; s/ //g; print $ProbTrajTable "Prob".$_."\t"."ErrProb".$_."\t"; } print $ProbTrajTable "\n"; for (my $i=0;$i<=38;$i++) { print $ProbTrajTable $FullGlobal{Time}[$i]."\t"; print $ProbTrajTable $FullGlobal{TH}[$i]."\t"; print $ProbTrajTable $FullGlobal{ErrTH}[$i]."\t"; print $ProbTrajTable $FullGlobal{H}[$i]."\t"; foreach my $State (@StateList) { print $ProbTrajTable $ProbState{$State}[$i]."\t".$ErrProbState{$State}[$i]."\t"; } print $ProbTrajTable "\n"; } close($ProbTrajTable);