in reply to Need to find a more efficient way to write if/elsif in loop

map
my @aoh = map { {row => $count, col => $_+2, input_value => $test1[$_]} } 0 .. 4; $googlesheet->batchupdate_cell(@aoh);

Replies are listed 'Best First'.
Re^2: Need to find a more efficient way to write if/elsif in loop
by AllPaoTeam (Sexton) on Sep 05, 2014 at 16:19 UTC
    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++

        0 .. $col_length;

        Should maybe be
            0 .. $col_length-1;
        based on OPed code.

      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;