複数のファイルから特定のデータをコピーします。その後、複数のデータを含む csv ファイルを作成します。

複数のファイルから特定のデータをコピーします。その後、複数のデータを含む csv ファイルを作成します。

私のコードからわかるように、各 par 値に対して 5 つの bor 値があるため、ループが 2 つあります。したがって、出力 .out 拡張子を持つファイルが 50 個あります。したがって、2 つのループを使用して、ファイルを自動的にコードにフィードしています。私の目標は、1 つの par 値の列が 1 つと、異なる bor 値ファイルから取得した異なる値が 5 列ある .csv ファイルを作成することです。これは、異なる par 値の行に対して続きます。これらの組み合わせのそれぞれについて、par 値は 5 つの bor 値すべてに対して一定のままですが、bor 値は組み合わせごとに変わります。したがって、各行に 6 つの列が必要で、列 1 には 5 つの異なる bor 値すべてに対して同じ定数値が含まれ、列 2 から列 6 にはそれらのファイルから取得した異なる値が含まれ

そのため、列 1 は単一の par 値のみで、残りの 5 列には 5 つの bor 値が必要です。コードを実行すると、if ステートメントを使用して、これらのファイルの特定の領域にある bor と par から必要なすべての値が出力されます。問題は、出力 .csv ファイルに何も出力されないことです。最後の par 値と最後の bor 値の組み合わせの値のみが出力されます。この場合は 1,3500 です。

    #!/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 = (0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1);
    #bor is my boron concentration list
    my @bor_list = (0,800,1600,2500,3500);
    # creating a variable for the current value of the parameter
    my $value;
    my $value_2;
    # get and store the size of the array
    my $nbr_of_values = $#par_list;
    my $nbr_of_values_2 = $#bor_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++){
        #create another loop for boron values and assign a letter j to it
        for ( my $j=0; $j<= $nbr_of_values_2; $j++){
        $value_2 = $bor_list[$j];
            $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_output_filename = $file_in."file_in_".$value."_".$value_2.".out";
            print " The new filename is $new_output_filename \n";
            my $result_filename = $file_in."_".".csv";

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

            while (<$fh_out>) {
            # 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 $_
            # now we actually print that line into the new file
            # BUT BEFORE THAT, we change the dummy characters for the real value
            # we use a regular expression (read the tutorials for more details_
            # s = substitute
                if ((/ COO /)&& (/                     INPUT/)) {
                print "found burnup $_ ";
                my @array = split(/\s+/,$_);
                #print "the bu value is $array[3] \n";
                print $fh_res "$array[2] ,";
                }
                if ((/   K-INF /) && (/M2 =/)) {
                print "found kinf $_ ";

                #print "the bu value is $array[3] \n";
                print $fh_res "$array[7] ,";
                }

            }
            close $fh_out; 
            close $fh_res;

         }
    }

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

答え1

あなたの特定の問題 (最後の値のみが出力ファイルに表示される) は、内部ループ内で書き込みモードで開くことによって発生していると思います。ファイルを開くための基本モードは、読み取り ( )、書き込み ( )、追加 ( )$fh_resの 3 つです。「書き込み」と「追加」の違いは、前者では既存の内容が破棄されるのに対し、「追加」では内容が保持されることです。'<''>''>>'

スニペットでは、csv ファイルのファイル名とファイル ハンドルを定義する行を、 を定義した直後のループ外に移動することをお勧めします$file_in

このスニペットが実際のものの簡略化されたバージョンであり、内部ループ内で csv ファイルを開いたり再度開いたりする正当な理由がある場合は、モード'>'(write) を'>>'(append) に置き換えることで問題を解決できると思います。

関連情報