Multiplication and iteration in solidity
Clash 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?
solidity remix gas
add a comment |Â
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?
solidity remix gas
add a comment |Â
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?
solidity remix gas
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
solidity remix gas
asked 5 hours ago
Cristian
546
546
add a comment |Â
add a comment |Â
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.
1
[OT] in Python you can write something like thisa = [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
add a comment |Â
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.
Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
â Cristian
3 hours ago
add a comment |Â
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.
1
[OT] in Python you can write something like thisa = [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
add a comment |Â
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.
1
[OT] in Python you can write something like thisa = [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
add a comment |Â
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.
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.
answered 4 hours ago
Lauri Peltonen
3,0531319
3,0531319
1
[OT] in Python you can write something like thisa = [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
add a comment |Â
1
[OT] in Python you can write something like thisa = [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
add a comment |Â
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.
Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
â Cristian
3 hours ago
add a comment |Â
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.
Thanks for the information. I was thinking if the language had this functionality like Python but seems to not.
â Cristian
3 hours ago
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password