Home   Help Search Login Register  

Author Topic: array elements into numbers  (Read 595 times)

0 Members and 3 Guests are viewing this topic.

bored_onion

  • Guest
array elements into numbers
« on: 27 Aug 2004, 15:23:50 »
if i have a 3 element array of which each is a number
eg:
[1,2,3]

how do i add these numbers together?
(in this case i would get 6)
i tried
Code: [Select]
_number = _array select 0 + _array select 1 + _array select 2
but it returned another array

thanks

Offline Blanco

  • Former Staff
  • ****
Re:array elements into numbers
« Reply #1 on: 27 Aug 2004, 17:23:36 »
I'm not an expert with arrays but try this :

Code: [Select]
_array = [1,2,3]
_result = 0

_i = 0

#L
_value = _array select _i
_result = _result + _value
_i = _i + 1
?_i < count _array : goto "L"

hint format ["DEBUG = %1",_result]


But there must be a much easier way...


« Last Edit: 27 Aug 2004, 17:24:33 by Blanco »
Search or search or search before you ask.

Offline Mud_Spike

  • Contributing Member
  • **
Re:array elements into numbers
« Reply #2 on: 27 Aug 2004, 17:37:57 »
Same concept as Blanco proposed but with less code:
Code: [Select]
_res = 0
"_res = _res + _x" foreach _array


Edit:
Just a note on your own example:

Code: [Select]
_number = _array select 0 + _array select 1 + _array select 2Because of operator precedence the result is not what you think it is, in such cases (or whenever you are unsure), use parens to group the individual expressions.:
Code: [Select]
_number = (_array select 0) + (_array select 1) + (_array select 2)
« Last Edit: 27 Aug 2004, 17:41:09 by Mud_Spike »

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:array elements into numbers
« Reply #3 on: 27 Aug 2004, 22:55:15 »
Just use parenthesis, and it should work:

_number = (_array select 0) + (_array select 1) + (_array select 2 )

Its better to overuse parenthesis then to under use them. Lots of frustration has been caused by me not using enough parenthesis before...
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

bored_onion

  • Guest
Re:array elements into numbers
« Reply #4 on: 28 Aug 2004, 15:24:33 »
cheers guys i appreciate the help

Offline dmakatra

  • Members
  • *
  • Better known as Armsty
Re:array elements into numbers
« Reply #5 on: 28 Aug 2004, 16:06:26 »
Just use parenthesis, and it should work:

_number = (_array select 0) + (_array select 1) + (_array select 2 )

Its better to overuse parenthesis then to under use them. Lots of frustration has been caused by me not using enough parenthesis before...

(_number = (((_array select 0)) + (((_array select 1))) + (_array select (2))))

 ;D

If the problem is solved then press the little solve button mate. :)

:beat: *Gets Shot* :beat:

Offline C0LDSt33L

  • Members
  • *
  • Member of the SotM Team
Re:array elements into numbers
« Reply #6 on: 28 Aug 2004, 16:11:54 »
He won't be able to solve until Tuesday (I think) because he's just gone off on some mini holiday. 8)