Jump to content

Modifying all values in an array


Recommended Posts

I am finally posting this after over an hour of searching through the forums and Google. I am not saying that a similar question doesn't exist, but the keywords I'm using to search keep bringing up questions involving adding new values to an array or pushing values over in an array.

What I'm trying to do is modify all numeric values in an array at once.

Example, instead of:

Local $i = MouseGetPos()
;assume this returns (800,600)

$i[0] += 5
;new coordinates are (805,600)

$i[1] += 5
;new coordinates are (805,605)

 

I am trying to modify all values inside at once, something similar to:

Local $i = MouseGetPos()
; assuming (800,600) again

$i += 5
;new coordinates (805,605)

But that isn't working.

 

Now for simple mouse coords this really isn't a big deal, but say I have a 3D array with many different variables, and I want to add +1 to every single value? It seems like such a simple answer to find and idk why I cant find it.

Link to comment
Share on other sites

 

37 minutes ago, mikell said:

Use nested For/Next loops  ^_^

#Include <Array.au3>

Local $a[2][2] = [[0,1],[10,20]]

For $i = 0 to UBound($a)-1
    For $j = 0 to UBound($a, 2)-1
        $a[$i][$j] += 5
    Next
Next
_ArrayDisplay($a)

 

I did find this solution in the UBound help file a bit after I posted this; it's not the concise solution I was looking for (like in my example), but it is much more concise than the alternative, so thank you v.much

Link to comment
Share on other sites

_Eigen_CwiseScalarOp_InPlace

And welcome to the forums.:)

Edited by RTFC
Link to comment
Share on other sites

And another example.

#include <Array.au3>

Local $i[2] = [800, 600] ;MouseGetPos()
_AA($i, "+ 5") ; Add 4 to all of arrays elements.
_ArrayDisplay($i)

Local $a[2][2] = [[0, 1], [10, 20]]
_AA($a, "*4")  ; multiply all array's elements by 4
_ArrayDisplay($a)

; --------- All of Array's elements -----------
; The array may be 1D or 2D.
;Any mathematical operator can be used in $sOp parameter.
Func _AA(ByRef $aArray, $sOp)
    If IsArray($aArray) Then
        If UBound($aArray, 0) = 2 Then
            For $i = 0 To UBound($aArray) - 1
                For $j = 0 To UBound($aArray, 2) - 1
                    $aArray[$i][$j] = Execute($aArray[$i][$j] & $sOp)
                Next
            Next
        Else
            For $j = 0 To UBound($aArray) - 1
                $aArray[$j] = Execute($aArray[$j] & $sOp)
            Next
        EndIf
    EndIf
EndFunc   ;==>_AA

 

Link to comment
Share on other sites

have to think there is a regex solution to be had...something heading down the path of

#Include <Array.au3>

Local $a[2][2] = [[0,1],[10,20]]

$s =  StringRegExpReplace(_ArrayToString($a) , "(\d+)" , "\1 + 5")
local $aNew[0][2]
_ArrayAdd($aNew , $s)
_ArrayDisplay($aNew)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...