How do I run the same command multiple times using Perl? -
i have 2 commands need run back 16 times 2 sets of data. have labeled files used file#a1_100.gen (set 1) , file#a2_100.gen (set 2). 100 replaced multiples of 100 upto 1600 (100,200,...,1000,...,1600).
example 1: first set
command 1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_100.gen file#a1_100.out
command 2: perl program2.pl file#a1_100.out file#a1_100.out.long
example 2: first set
command 1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_200.gen file#a1_200.out
command 2: perl program2.pl file#a1_200.out file#a1_200.out.long
these 2 commands repeated 16 times both set 1 , set 2. set 2 filename changes file#a2...
i need command run on own changing filename 2 sets, running 16 times each set.
any appreciated! thanks!
this done shell script. perl, tmtowtdi — there's more 1 way it.
for num in $(seq 1 16) perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_${num}00.gen file#a1_${num}00.out perl myprogram2.pl file#a1_${num}00.out file#a1_${num}00.out.long done
(you use {1..16}
in place of $(seq 1 16)
generate numbers. might note #
characters in file names discombobulate markdown system.)
or use:
for num in $(seq 100 100 1600) perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_${num}.gen file#a1_${num}.out perl myprogram2.pl file#a1_${num}.out file#a1_${num}.out.long done
(i don't think there's {...}
expansion that.)
or, better, use variables hold values avoid repetition:
pos="file#a1.pos" abc="abc#a1.ref" xyz="xyz#a1.ref" num in $(seq 100 100 1600) pfx="file#a1_${num}" gen="${pfx}.gen" out="${pfx}.out" long="${out}.long" perl myprogram1.pl "${pos}" "${abc}" "${xyz}" "${gen}" "${out}" perl myprogram2.pl "${out}" "${long}" done
in code, braces around parameter names optional; in first block of code, braces around ${num}
mandatory, optional in second set. enclosing names in double quotes optional here, recommended.
or, if must in perl, then:
use warnings; use strict; $pos = "file#a1.ref"; $abc = "abc#a1.ref"; $xyz = "xyz#a1.ref"; (my $num = 100; $num <= 1600; $num += 100) { $pfx = "file#a1_${num}"; $gen = "${pfx}.gen"; $out = "${pfx}.out"; $long = "${out}.long"; system("perl", "myprogram1.pl", "${pos}", "${abc}", "${xyz}", "${gen}", "${out}"); system("perl", "myprogram2.pl", "${out}", "${long}"); }
this pretty basic coding. , can guess didn't take me long generate last shell script. note use of multiple separate strings instead on 1 long string in system
calls. avoids running shell interpreter — perl runs perl
directly.
you use $^x
instead of "perl"
ensure run same perl executable ran script shown. (if have /usr/bin/perl
on path
run $home/perl/v5.20.1/bin/perl thescript.pl
, difference might matter, wouldn't.)
Comments
Post a Comment