2016-10-12 9 views
-4

教授が与えたperlスクリプトがありますが、これは実行しやすいと思われますが、私のマシンはエラーを出しています。 このファイルが入っているディレクトリは、私が作成したfile_inという名前の別のファイルを持っています。なぜなら、私はこのスクリプトを実行するために必要なことだと思うからです。でも33行目にエラーが出ます。助けてください。 ありがとう、誰かが簡単なperlスクリプトで私を助けることができます

#!/usr/bin/perl 

# the strict package forces you to declare each variable you use beforehand 
use strict; 

# a variable in strict mode is declared using my 
# the $ symbol means it is a single-valued variable 
# the @ symbol means it is an array 
# each declaration/instruction is closed with a ; sign 
my @par_list = (1,2,3,4,5,6,7,8,9,10); 

# creating a variable for the current value of the parameter 
my $value; 

# get and store the size of the array 
my $nbr_of_values = $#par_list; 

# now, we read in a variable that will be the filename of the template input file 
# $ARGV are the input arguments, 0 means it is the first one (perl starts counting at 0, not 1) 
my $file_in = $ARGV[0]; 

# start of the loop 
for(my $i=0; $i<= $nbr_of_values; $i++){ 
    $value = $par_list[$i]; 
    print "This is the current parameter value: $value \n"; 

    # now we create a new string variable that will later be the filename of the new input deck 
    # the . symbol is the concatenation operator between strings 
    my $new_input_filename = $file_in."_".$value; 
    print " The new filename is $new_input_filename \n"; 

    # open the template file and store its filehandle (fh_in) 
    open my $fh_in, '<', $file_in or die "Can't open output $file_in !"; 
    # open the new file (it currently does not exist and is thus empty) and store its filehandle (fh_out) 
    open my $fh_out, '>', $new_input_filename or die "Can't open output $new_input_filename !"; 

    while (<$fh_in>) { 
    # this is for you to see on the console, we read line-by-line, while there is something 
    # the line read is stored in a special PERL variable $_ 
    print "I have read $_"; 
    # now we actually print that line intot he new file 
    print $fh_out $_; 
    } 
    close $fh_in; 
    close fh_out; 
} 

print " I am done with this !!! \n"; 
exit 111; 
+1

を実行しますか?そのエラーメッセージには、おそらく問題の原因に関する重要な手掛かりがあります。 – dasgar

+0

ほんの他のアイテム。 「警告を使用する」と役に立ちます。有用な警告を提供するのに役立つかもしれない "use strict;"の後の行にある。変数$ nbr_of_valuesは、実際にコメントが示唆するように配列のサイズを取得していません。代わりに、配列の最後の要素IDを取得しています。また、33行目が最初のopen文に対応しているように見えます。もしあなたが変数$!単語dieの後の二重引用符の中に、Perlがファイルをオープンできなかった理由に関する情報を示すエラーメッセージが表示されます。 – dasgar

+0

あなたのスクリプトが読んでいるはずの10のファイルがありますか?終了コードのエラーメッセージは、終了コード111で終了していますか? – AntonH

答えて

1

このスクリプトではfile_inは開かれません。 $file_inはスクリプトに渡すべき変数です。注my $file_in = $ARGV[0];

$ARGV[0]は、スクリプトに渡す必要がある最初のコマンドライン引数です。

How do you use command line parameters

あなたはそのディレクトリ内のファイルを作成し、「file_in」と呼んでいる場合は、あなたが取得しているエラーメッセージは何perl_part3.pl file_in

+0

Shabash Boi Shabadh。 –

+1

それは最高だった..私のupvoteを取る –

+0

他はそれを取得しなかったが、あなたはポイントにあった。 –

関連する問題