Sign in to follow this  
Followers 0
jftuga

Adding arrays

22 posts in this topic




If you want every element of Array $a added with the same element of Array $b it could look like this (I'm not a Pro)

For $start=0 To 99
  $c[$start]=$a[$start]+$b[$start]
Next

If you want the contets of $b should be written in $c after the contets of $a it could look like this

#include <Array.au3>

$c=$a
For $start=0 To 99
  _ArrayInsert($c, $b[$start], $start+100)
Next

Share this post


Link to post
Share on other sites

I hope $c=$a doesn't change the size of $c. Else you should use a second Loop.

Share this post


Link to post
Share on other sites

Here, check this out... accounts for arrays of different, unknown sizes:

; Combine 2 arrays
; _CombineArray(first array, second array)
; Returns new array

Func _CombineArray($Array1, $Array2)
    Local $NewArray = $Array1, $i
    ReDim $NewArray[UBound($Array1) + UBound($Array2)]
    For $i = 0 To UBound($Array2) - 1
        $NewArray[UBound($Array1) + $i] = $Array2[$i]
    Next
    Return $NewArray
EndFuncoÝ÷ ØLZ^jëh×6#include <array.au3>  ; just for _ArrayDisplay

#region - Just creating array a and b for example
Dim $a[15], $b[10]

For $i = 0 To UBound($a) - 1
    $a[$i] = 'a ' & $i
Next

For $i = 0 To UBound($B) - 1
    $b[$i] = 'b ' & $i
Next
#endregion

$c = _CombineArray($a, $B)

_ArrayDisplay($c, 'Combined Array')

Func _CombineArray($Array1, $Array2)
    Local $NewArray = $Array1, $i
    ReDim $NewArray[UBound($Array1) + UBound($Array2)]
    For $i = 0 To UBound($Array2) - 1
        $NewArray[UBound($Array1) + $i] = $Array2[$i]
    Next
    Return $NewArray
EndFunc

Share this post


Link to post
Share on other sites

Here, check this out... accounts for arrays of different, unknown sizes:

; Combine 2 arrays
; _CombineArray(first array, second array)
; Returns new array

Func _CombineArray($Array1, $Array2)
    Local $NewArray = $Array1, $i
    ReDim $NewArray[UBound($Array1) + UBound($Array2)]
    For $i = 0 To UBound($Array2) - 1
        $NewArray[UBound($Array1) + $i] = $Array2[$i]
    Next
    Return $NewArray
EndFunc
Xcal, this really is wonderful. I wonder why it's not in the Array UDF. It should be.

A decision is a powerful thing

Share this post


Link to post
Share on other sites

No problem. I just made it, and I just woke up, so I hope it works consistantly. :)

I'm looking at it quickly... but shouldn't

ReDim $NewArray[UBound($Array1) + UBound($Array2)]oÝ÷ غÚ"µÍQ[H  ÌÍÓ]Ð^VÊPÝ[
    ÌÍÐ^LJH
ÈPÝ[
    ÌÍÐ^LJHHW
?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

smOke_N, it seems to be working fine. I tested it. However, on first glance I thought the same thing. But the loop seems to be taking care of that. Maybe I'm totally wrong. I'm going to do some more testing and Xcal let us know what you think.

Thanks gang!


A decision is a powerful thing

Share this post


Link to post
Share on other sites

You would think so, but I get those subscripts (incorrect/exceeded) errors if I do that.

Yeah, I've never understood why that happens. However, what you've done is a similar "workaround" I've used.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

You would think so, but I get those subscripts (incorrect/exceeded) errors if I do that.

You get the error because of:

$NewArray[uBound($Array1) + $i] << (You're not taking away 1 for the UBound... What would be faster for larger arrays is getting the UBound first ... I have this same function in my snippets, I have it as:

Func _ArrayAddArray(ByRef $aMain, $aAdd, $iBase = 1)
    If Not IsArray($aMain) Or Not IsArray($aAdd) Then Return SetError(1, 0, 0)
    Local $iAddTo = UBound($aMain) - 1
    ReDim $aMain[UBound($aMain) + (UBound($aAdd) - 1)]
    For $iCC = $iBase To UBound($aAdd) - 1
        $iAddTo += 1
        $aMain[$iAddTo] = $aAdd[$iCC]
    Next
    Return 1
EndFunc

Though... It doesn't return an array, just merges the two. It will be faster simply because I'm not checking an outside function on ever loop (Ubound).


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Well, now that I think about it, it makes sense to leave off the -1.

if you do...

Dim $a[10]

MsgBox(0, '', UBound($a))

...the message box shows 10. So you want the original Dim-ed size of of the two arrays added together to be the size of the new array. Of course you need the -1 if you're running an array through a loop, since if you do 0 To Ubound($a) it'll be 11 iterations (10 + the zero).

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

So I guess the best way would be to do...

Func _CombineArray($Array1, $Array2)
    Local $NewArray = $Array1, $i, $element = UBound($Array1)
    ReDim $NewArray[UBound($Array1) + UBound($Array2)]
    For $i = 0 To UBound($Array2) - 1
        $NewArray[$element + $i] = $Array2[$i]
    Next
    Return $NewArray
EndFunc

(edit - adding in the IsArray() error checking would be a good idea, like smoke has, of course. And by "best," I mean best for the way I did it. :) )

Edited by xcal

Share this post


Link to post
Share on other sites

So I guess the best way would be to do...

Func _CombineArray($Array1, $Array2)
    Local $NewArray = $Array1, $i, $element = UBound($Array1)
    ReDim $NewArray[UBound($Array1) + UBound($Array2)]
    For $i = 0 To UBound($Array2) - 1
        $NewArray[$element + $i] = $Array2[$i]
    Next
    Return $NewArray
EndFunc

(edit - adding in the IsArray() error checking would be a good idea, like smoke has, of course.)

Still leaves issues if you ask me... [0] may contain the size of the number 2 array and not actual information...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

In case anyone cares. I see both of these are good things to add to the Array UDF (they both serve different purposes).

;===============================================================================
;
; Function Name:    _ArrayAddArray()
; Description:      Adds an array's elements to another specified array
; Parameter(s):     $aMain  - The array to add elements to.
;                   $aAdd   - The array to add from.
;
; Requirement(s):   None.
; Return Value(s):  On Success - 1
;                   On Failure - 0
; Author(s):      SmOke_N
;
;===============================================================================
Func _ArrayAddArray(ByRef $aMain, $aAdd, $iBase = 1)
    If Not IsArray($aMain) Or Not IsArray($aAdd) Then Return SetError(1, 0, 0)
    Local $iAddTo = UBound($aMain) - 1
    ReDim $aMain[UBound($aMain) + (UBound($aAdd) - 1)]
    For $iCC = $iBase To UBound($aAdd) - 1
        $iAddTo += 1
        $aMain[$iAddTo] = $aAdd[$iCC]
    Next
    Return 1
EndFuncoÝ÷ Ù«­¢+Øìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ì(ìչѥ½¸9µè}
½µ¥¹ÉÉä ¤(ìÍÉ¥ÁÑ¥½¸è
½µ¥¹ÑݼÉÉä±µ¹ÑÌ¥¹Ñ¼½¹¹ÜÉÉä(ìAɵÑȡ̤èÀÌØíÉÉäÄ´Q¡¥ÉÍÐÉÉä¸(ìÀÌØíÉÉäÈ´Q¡Í½¹ÉÉä¸(ì(ìIÅեɵ¹Ð¡Ì¤è9½¹¸(ìIÑÕɸY±Õ¡Ì¤èQ¡½µ¥¹ÉÉä(ìÕÑ¡½È¡Ì¤èa°(ì9½Ñ¡Ì¤è9½¹¸(ì(ìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô()Õ¹}ÉÉå
½µ¥¹ ÀÌØíÉÉäÄ°ÀÌØíÉÉäȤ(1½°ÀÌØí9ÝÉÉäôÀÌØíÉÉäÄ°ÀÌØí¤°ÀÌØí±µ¹ÐôU ½Õ¹ ÀÌØíÉÉäĤ(I¥´ÀÌØí9ÝÉÉåmU  ½Õ¹ ÀÌØíÉÉäĤ¬U    ½Õ¹ ÀÌØíÉÉäÈ¥t(½ÈÀÌØí¤ôÀQ¼U   ½Õ¹ ÀÌØíÉÉäȤ´Ä(ÀÌØí9ÝÉÉålÀÌØí±µ¹Ð¬ÀÌØí¥tôÀÌØíÉÉäÉlÀÌØí¥t(9áÐ(IÑÕɸÀÌØí9ÝÉÉä)¹Õ¹

A decision is a powerful thing

Share this post


Link to post
Share on other sites

Still leaves issues if you ask me... [0] may contain the size of the number 2 array and not actual information...

Ya, I see what you mean, but I wasn't really concerning myself with what the arrays contained. My only objective was to just combine them raw.

Share this post


Link to post
Share on other sites

Ya, I see what you mean, but I wasn't really concerning myself with what the arrays contained. My only objective was to just combine them raw.

I don't see what he means (dang it).

I'll do some testing. Maybe that will clear things up.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

ok I think I get it. It's because [0] isn't the size of the array, thus the size isn't correct either.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0