Multiplication and iteration in solidity

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Iteration uses a lot of gas. I was thinking at different options I could minimise the costs. I must understand why one thing doesn't work:
Why can't I push into an array using multiplication rather than loops?



For example, this code works:



address contenders

for (int i; i<5;i++)
contenders.push(msg.sender);


This code doesn't:



address contenders
uint numberToMultiply /// let's say this variable is 5

contenders.push(msg.sender) * numberToMultiply


Basically in the last example I am expecting to push msg.sender in the array by the number of times stored in the variable.



Would it not be gas efficient? Furthermore, it doesn't work. Does anyone know the reason?










share|improve this question

























    up vote
    1
    down vote

    favorite












    Iteration uses a lot of gas. I was thinking at different options I could minimise the costs. I must understand why one thing doesn't work:
    Why can't I push into an array using multiplication rather than loops?



    For example, this code works:



    address contenders

    for (int i; i<5;i++)
    contenders.push(msg.sender);


    This code doesn't:



    address contenders
    uint numberToMultiply /// let's say this variable is 5

    contenders.push(msg.sender) * numberToMultiply


    Basically in the last example I am expecting to push msg.sender in the array by the number of times stored in the variable.



    Would it not be gas efficient? Furthermore, it doesn't work. Does anyone know the reason?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Iteration uses a lot of gas. I was thinking at different options I could minimise the costs. I must understand why one thing doesn't work:
      Why can't I push into an array using multiplication rather than loops?



      For example, this code works:



      address contenders

      for (int i; i<5;i++)
      contenders.push(msg.sender);


      This code doesn't:



      address contenders
      uint numberToMultiply /// let's say this variable is 5

      contenders.push(msg.sender) * numberToMultiply


      Basically in the last example I am expecting to push msg.sender in the array by the number of times stored in the variable.



      Would it not be gas efficient? Furthermore, it doesn't work. Does anyone know the reason?










      share|improve this question













      Iteration uses a lot of gas. I was thinking at different options I could minimise the costs. I must understand why one thing doesn't work:
      Why can't I push into an array using multiplication rather than loops?



      For example, this code works:



      address contenders

      for (int i; i<5;i++)
      contenders.push(msg.sender);


      This code doesn't:



      address contenders
      uint numberToMultiply /// let's say this variable is 5

      contenders.push(msg.sender) * numberToMultiply


      Basically in the last example I am expecting to push msg.sender in the array by the number of times stored in the variable.



      Would it not be gas efficient? Furthermore, it doesn't work. Does anyone know the reason?







      solidity remix gas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      Cristian

      546




      546




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values.



          Because commands are executed from left to right, the contenders.push(msg.sender) is executed first. It returns the new length of the array (What is the return of array.push() in Solidity?). Therefore you are trying to execute something like "number * 5", which actually should give you a result if you store it in a variable - but it's not executing anything except the calculation.






          share|improve this answer
















          • 1




            [OT] in Python you can write something like this a = [1]*27
            – Briomkez
            4 hours ago










          • Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
            – Cristian
            3 hours ago

















          up vote
          1
          down vote













          The code doesn't work because it was not designed to work that way. You are missing the concept.



          From solidity documentation




          push: Dynamic storage arrays and bytes (not string) have a member
          function called push that can be used to append an element at the end
          of the array. The function returns the new length.




          Array.push return new length of the array. So multiplying it by 5 won't insert the data 5 times.






          share|improve this answer




















          • Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
            – Cristian
            3 hours ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "642"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f58769%2fmultiplication-and-iteration-in-solidity%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values.



          Because commands are executed from left to right, the contenders.push(msg.sender) is executed first. It returns the new length of the array (What is the return of array.push() in Solidity?). Therefore you are trying to execute something like "number * 5", which actually should give you a result if you store it in a variable - but it's not executing anything except the calculation.






          share|improve this answer
















          • 1




            [OT] in Python you can write something like this a = [1]*27
            – Briomkez
            4 hours ago










          • Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
            – Cristian
            3 hours ago














          up vote
          3
          down vote



          accepted










          I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values.



          Because commands are executed from left to right, the contenders.push(msg.sender) is executed first. It returns the new length of the array (What is the return of array.push() in Solidity?). Therefore you are trying to execute something like "number * 5", which actually should give you a result if you store it in a variable - but it's not executing anything except the calculation.






          share|improve this answer
















          • 1




            [OT] in Python you can write something like this a = [1]*27
            – Briomkez
            4 hours ago










          • Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
            – Cristian
            3 hours ago












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values.



          Because commands are executed from left to right, the contenders.push(msg.sender) is executed first. It returns the new length of the array (What is the return of array.push() in Solidity?). Therefore you are trying to execute something like "number * 5", which actually should give you a result if you store it in a variable - but it's not executing anything except the calculation.






          share|improve this answer












          I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values.



          Because commands are executed from left to right, the contenders.push(msg.sender) is executed first. It returns the new length of the array (What is the return of array.push() in Solidity?). Therefore you are trying to execute something like "number * 5", which actually should give you a result if you store it in a variable - but it's not executing anything except the calculation.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          Lauri Peltonen

          3,0531319




          3,0531319







          • 1




            [OT] in Python you can write something like this a = [1]*27
            – Briomkez
            4 hours ago










          • Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
            – Cristian
            3 hours ago












          • 1




            [OT] in Python you can write something like this a = [1]*27
            – Briomkez
            4 hours ago










          • Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
            – Cristian
            3 hours ago







          1




          1




          [OT] in Python you can write something like this a = [1]*27
          – Briomkez
          4 hours ago




          [OT] in Python you can write something like this a = [1]*27
          – Briomkez
          4 hours ago












          Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
          – Cristian
          3 hours ago




          Exactly. It's because I'm coming from python so that's why I thought it would work this way. Thanks all for the answers. I make sure to leave a big up for you all
          – Cristian
          3 hours ago










          up vote
          1
          down vote













          The code doesn't work because it was not designed to work that way. You are missing the concept.



          From solidity documentation




          push: Dynamic storage arrays and bytes (not string) have a member
          function called push that can be used to append an element at the end
          of the array. The function returns the new length.




          Array.push return new length of the array. So multiplying it by 5 won't insert the data 5 times.






          share|improve this answer




















          • Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
            – Cristian
            3 hours ago














          up vote
          1
          down vote













          The code doesn't work because it was not designed to work that way. You are missing the concept.



          From solidity documentation




          push: Dynamic storage arrays and bytes (not string) have a member
          function called push that can be used to append an element at the end
          of the array. The function returns the new length.




          Array.push return new length of the array. So multiplying it by 5 won't insert the data 5 times.






          share|improve this answer




















          • Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
            – Cristian
            3 hours ago












          up vote
          1
          down vote










          up vote
          1
          down vote









          The code doesn't work because it was not designed to work that way. You are missing the concept.



          From solidity documentation




          push: Dynamic storage arrays and bytes (not string) have a member
          function called push that can be used to append an element at the end
          of the array. The function returns the new length.




          Array.push return new length of the array. So multiplying it by 5 won't insert the data 5 times.






          share|improve this answer












          The code doesn't work because it was not designed to work that way. You are missing the concept.



          From solidity documentation




          push: Dynamic storage arrays and bytes (not string) have a member
          function called push that can be used to append an element at the end
          of the array. The function returns the new length.




          Array.push return new length of the array. So multiplying it by 5 won't insert the data 5 times.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          Prashant Prabhakar Singh

          3,73631546




          3,73631546











          • Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
            – Cristian
            3 hours ago
















          • Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
            – Cristian
            3 hours ago















          Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
          – Cristian
          3 hours ago




          Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
          – Cristian
          3 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f58769%2fmultiplication-and-iteration-in-solidity%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery