First, your inner loop reads the ODIR directory all the way through, so there's nothing to read in the second iteration of the outer loop. Second, since you've opened the directory and are putting files in it while you have it open, I don't know that there's any guarantee that you won't miss a file. You could fix both problems by opening ODIR just before the start of the second loop. Alternatively, you could use a glob to find the matching filenames, rather than scanning through all of them.

However, there's a better way: Since you already know the name of the file, there's no reason to scan through the directory for it. Instead, just open your file. Since you pass in $sample on the command line, there's no reason to use the opendir / readdir and regular expression.

my $cmd1 = "java -Xmx2G -jar $rootdir/celConverter/CelFileConverter.ja +r -m $rootdir/celConverter/Snp6FeatureMappings.csv -c $rootdir/cdf/Ge +nomeWideSNP_6.fromDB.cdf -s $celdir/ -t $rootdir/outdir/raw"; system($cmd1); my $dir = "$rootdir/outdir/output2/$sample" . "_feature.TXT"; opendir(DIR, "$rootdir/outdir/raw") || "Cannot open the directory $!\n +"; foreach my $file (readdir(DIR)){ if($file =~ /feature_intensity$/){ #my $cmd = "sh run_preprocessing.sh $rootdir/Matlab_Compilet_Runti +me/v710/ $file $rootdir/info/ $rootdir/outdir/raw/ $rootdir/outdir/ou +tput/ $rootdir/outdir/ PRIMARY 0.5"; #warn "Running $cmd... \n"; #system($cmd); #outputs ploidy_3456_feature.TXT.csv #output from the previous system cmd. $file2 = "ploidy_" . $sample . "_feature.TXT.csv"; open FILE, "<", $dir/$file2" or "Cannot open the file $file2 $!\n"; while(<FILE>){ chomp; my ($in_pi,$in_ploidy,$in_alpha) = split(/,/); my $cmd2 = "sh run_HMM.sh $rootdir/Matlab_Compilet_Runtime/v710/ + $file $rootdir/info/ $rootdir/outdir/output/ $rootdir/outdir/ 10 $in +_pi $in_ploidy $in_alpha"; warn "Running $cmd2\n"; system($cmd2); } close FILE; } }

I've cleaned up your indentation a little to make the code easier to follow, and used the three-argument form of open, as a reminder that it's generally accepted as the better form to use.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Running executables with perl using system command by roboticus
in thread Running executables with perl using system command by Anonymous Monk

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.