AllPaoTeam has asked for the wisdom of the Perl Monks concerning the following question:

Im reading from a file and depends on the length of the file, it will write to google spreadsheet. I have many files which range from 5 elements in an array to 200 elements(as u can see in the example I only have up to 15) elements. Right now its taking me a long time to write the code, is there an easier way to write it? Below is the part of the code of the looping strucure:
#$col_length depends on the intial read in of the file to see +how many elements in each line $count = 6; while (<GOOG>) { @test1 = split(/ /); if ($col_length == 5) { $googlesheet->batchupdate_cell( {row => $count, col => 2, input_value => $test1[0]}, {row => $count, col => 3, input_value => $test1[1]}, {row => $count, col => 4, input_value => $test1[2]}, {row => $count, col => 5, input_value => $test1[3]}, {row => $count, col => 6, input_value => $test1[4]} ); } elsif ($col_length == 10) { $googlesheet->batchupdate_cell( {row => $count, col => 2, input_value => $test1[0]}, {row => $count, col => 3, input_value => $test1[1]}, {row => $count, col => 4, input_value => $test1[2]}, {row => $count, col => 5, input_value => $test1[3]}, {row => $count, col => 6, input_value => $test1[4]}, {row => $count, col => 7, input_value => $test1[5]}, {row => $count, col => 8, input_value => $test1[6]}, {row => $count, col => 9, input_value => $test1[7]}, {row => $count, col => 10, input_value => $test1[8]}, {row => $count, col => 11, input_value => $test1[9]} ); } elsif ($col_length == 15) { $googlesheet->batchupdate_cell( {row => $count, col => 2, input_value => $test1[0]}, {row => $count, col => 3, input_value => $test1[1]}, {row => $count, col => 4, input_value => $test1[2]}, {row => $count, col => 5, input_value => $test1[3]}, {row => $count, col => 6, input_value => $test1[4]}, {row => $count, col => 7, input_value => $test1[5]}, {row => $count, col => 8, input_value => $test1[6]}, {row => $count, col => 9, input_value => $test1[7]}, {row => $count, col => 10, input_value => $test1[8]}, {row => $count, col => 11, input_value => $test1[9]}, {row => $count, col => 12, input_value => $test[10]}, {row => $count, col => 13, input_value => $test[11]}, {row => $count, col => 14, input_value => $test[12]}, {row => $count, col => 15, input_value => $test[13]}, {row => $count, col => 16, input_value => $test[14]} + ); } $count++;

Replies are listed 'Best First'.
Re: Need to find a more efficient way to write if/elsif in loop
by toolic (Bishop) on Sep 05, 2014 at 15:54 UTC
    map
    my @aoh = map { {row => $count, col => $_+2, input_value => $test1[$_]} } 0 .. 4; $googlesheet->batchupdate_cell(@aoh);
      Ok I tried your code, how come it only wrote 5 elements instead of 10 elements from the file? I do not understand what the "$_+2" means. Thanks! Below is the code I am testing with:
      $AZ_len = 10; &update_google ($az_state, $AZ_len); sub update_google { my($infile, $col_length) = @_; #Open files to read from open GOOG, "<", $infile or die $!; $count = 6; while (<GOOG>) { @test1 = split(/ /); my @aoh = map { {row => $count, col => $_+2, input_value => $test1[$_]} } 0 .. 4; $googlesheet->batchupdate_cell(@aoh); $count++; } close(GOOG); } # $infile looks like this 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 5 0 0 0 0 0 0 0 0 4 6 0 0 0 0 0 0 0 0 5 4 0 0 0 0 0 0 0 0 0 0 0 0 0 #Google spreadsheet only prints this 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        To make it more general, use
        my @aoh = map { {row => $count, col => $_+2, input_value => $test1[$_]} } 0 .. ($col_length-1);

        UPDATE: changed code due to error identified by AnomalousMonk++

        Duh.... I figured it out.... Thanks for the help once again guys! I didn't see the code below, my eyes just flew passed it:
        0 .. 4;
Re: Need to find a more efficient way to write if/elsif in loop
by AppleFritter (Vicar) on Sep 05, 2014 at 15:56 UTC

    I'd suggest using a loop, along the following lines:

    my @params; for my $col (2..$col_length + 1) { push @params, { row => $count, col => $col, input_value => $test1[ +$col - 2] }; } $googlesheet->batchupdate_cell(@params);

    EDIT: or employ map as toolic suggested, that's actually more idiomatic.

      Thanks guys, I will check out Map.