電子書籍の厳選無料作品が豊富!

&あああ("ファイル名1", "AGT");
&あああ("ファイル名2", "ACA");
&あああ("ファイル名3", "TAT");
&あああ("ファイル名4", "TGA");
sub あああ{
open(FILE, "処理させるファイル") or die "$!";
my ($file, $tag) = @_;
open(NEWFILE, "> $file") or die "$!";
my $x = 0;
my @ti;
while(<FILE>){
if($_ =~ "^>"){
$x ++;
$ti[$x] = $_;
} elsif($_ =~ "^$tag"){
print NEWFILE $ti[$x].$_;
}
}
print "$x\n";
close NEWFILE;
}



このプログラムをサブルーチンではなく
forかなんかで実行させ
自由にファイル数と$tagを設定し
実行できるようにしたいのですが
できますでしょうか><

forをまわす回数を指定しSTDINなんかで指定し
そのforの中でファイル処理に必要な条件や
吐き出すファイルの名前をSTDINで指定するようなものを書きたいです



ちなみにperl初心者です
質問が意味不明かもしれないです><
よろしくおねがいします

A 回答 (2件)

以下を組み合わせたら何かしら出来るかと思います。



# STDINで指定
chomp( my $stdin = <STDIN> );

# forで$stdin回まわす
my $name = "hoge";
for (1..$stdin) { print $name . $_ , "\n"; }

# Tagを回す
my @tags = qw(AGT ACA TAT TGA);
foreach my $tag (@tags) { print "$tag\n"; }
    • good
    • 0

コマンドラインのオプションを取り扱いたいんだと想像します。


次のスクリプトでは次のオプションが使えます。
-t LIST or --tag=LIST コンマ区切りのタグ文字列を指定します。複数回指定できます。
-o PATH or --ouput=PATH 出力ファイルのプリフィクスです。デフォルトはスクリプトの名前+'-output-'。
-n or --num 出力ファイル名の連番を数字にします。デフォルトはアルファベットです。

例:
$ perl arg.pl --tag=a,b,c
arg-output-a
a
arg-output-b
b
arg-output-c
c

$ perl arg.pl --tag=a --tag=b --tag=c --output=path-
path-a
a
path-b
b
path-c
c

$ perl arg.pl -t a,b,c -o path- --num
path-1
a
path-2
b
path-3
c

arg.pl:
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use File::Basename;
use Getopt::Long;
my @tag;
my $output = basename($0, '.pl') . '-output-';
my $num;
GetOptions(
  'tag=s' => \@tag
 , 'output=s' => \$output
 , 'num' => \$num
);
@tag = split(/,/, join(',', @tag));
my $suffix = 'a';
if ($num) { $suffix = '1'; }
foreach (@tag) { &aaa($output.$suffix++, $_); }
sub aaa { # 単に引数をプリントするだけ
 my ($output, $tag) = @_;
 say $output; say $tag;
}

参考URL:http://perldoc.perl.org/Getopt/Long.html
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!