Since you can see from my code, i have two loops because for each par value there are 5 bor values. so i have 50 files with the output .out extension. So i'm using two loops to feed in the files to the code automatically. My objective is to make a .csv file which has one column of 1 par value, and 5 columns of different values i grab from different bor values files, and that goes on for rows of different par values. For each of these combinations my par value remains constant for all 5 bor values, but my bor value changes for every combination.So i need 6 columns on each row, column one would have the same constant value of all 5 different bor values, and column 2 to column 6 will have different values that i will grab from those files.

So that's why column one should be just the single par value with 5 bor values in the remaining 5 columns. When i run my code, it prints me all the values that i need from bor and par, which are in specific areas on those files using the if statement. The problem is it doesn't print anything in my output .csv file. It just prints the values of the combination of last par value with last bor value. which in this case is 1,3500


    # 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;

I think that your specific problem (only the last value appears in the output file) is caused by opening $fh_res in write mode inside the inner loop. There are 3 basic modes for opening a file: read ('<'), write ('>') and append ('>>'). The difference between 'write' and 'append' is that with the former you discard any existing contents while with 'append' they are kept.

In your snippet I would suggest to move the lines for defining the file name and file handle of the csv file outside of the loops, directly after defining $file_in.

If this snippet is actually a simplified version of the real thing and you have some good reasons for opening and re-opening the csv file within the inner loop, then I think you can solve your problem by replacing the mode '>' (write) by '>>' (append).

  • Hey the append function is working awesome but its producing files like – J. Doe Oct 14 '16 at 12:34
  • 0.1, 1.5 0.1, 1.7 0.2, 1.6 0.2, 1.7 is there anyway to do 0.1, 1.5,1.7 0.2,1.6,1.7 ? Thanks your append helped me with getting all the values in csv – J. Doe Oct 14 '16 at 12:35
  • 0.1, 1.5 \n 0.1, 1.7 \n 0.2, 1.6 \n 0.2, 1.7 \n is there anyway to do 0.1, 1.5,1.7 \n 0.2,1.6,1.7 \n ? Thanks your append helped me with getting all the values in csv – J. Doe Oct 14 '16 at 12:37
  • I'm not sure if I understand correctly what you would like to get in your csv file, but my guess would be that you want one line per bor/par combination. I think you could achieve this by: (1) inserting my $line = "$value,$value_2,"; just before the while (<$fh_out>) loop, (2) replacing (twice) print $fh_res by $line .= within the while loop, (3) adding `print $fh_res "$line\n"; after the while loop. I did not test this. It's many years ago since I last did some perl, I am a python addict nowadays. :) – davino Oct 15 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.