in reply to sub will not return data correctly (the way i want it to!!)

The return is executed after the 1st iteration of the for loop. This means you exit the sub after only 1 time through the loop, not 4 times.
  • Comment on Re: sub will not return data correctly (the way i want it to!!)

Replies are listed 'Best First'.
Re^2: sub will not return data correctly (the way i want it to!!)
by james28909 (Deacon) on Jun 18, 2015 at 00:28 UTC
    Oh, ok i was unaware of that until now. I guess i will push all data onto an array then as said in CB.

      You can also call the function 4 times instead:

      my @test_1_data = test_1($test); my @test_2_data; push @test_2_data, test_1( $test ) for 1 .. 4;
      Notice that test_2() is not even called. A for loop around the first sub accomplishes the same goal.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        thats wild that it doesnt even need test_2() called. I tried it and it did work, but why does it work even when test_2() isnt called? I will more than likely use this, otherwise I will have to change up abunch of code.
        EDIT: i found another problem as well ;) I also am converting the data to its hex equivalent with unpack('H*', ()).

        Why does this fail:
        push @test_2_data, unpack('H*', (test_2( $test ))) for 1 .. 4; print $_ for(@test_2_data);

        And this works:
        push @test_2_data, test_2( $test ) for 1 .. 4; for(@test_2_data){ $_ = unpack('H*', $_); } print $_ for(@test_2_data);