Jump to content

Load same value into range of array indexes


Go to solution Solved by SmOke_N,

Recommended Posts

I want to load an array with a preset list of values with some unique, and others repeating.  Something like:

Global $arr[3][28]

$arr[0][0] = "foo0"

$arr[0][1] = "foo1"
$arr[0][2] = "foo2"
$arr[0][3] = "foo3"
$arr[0][4..7] = "foo4"
$arr[0][8..9] = "foo5"
$arr[0][10..27] = "foo6"
 
Explicitly defining the list of 28 items stinks, and For Next loops isn't much better.  I've searched forums with no luck, and haven't been able to find a built in function that performs this way.  Is there an 'elegant' way to do this?
Link to comment
Share on other sites

 

You mean somethin like this ?

Global $arr[28]

For $i = 0 To UBound($arr) - 1
    $arr[$i] = "foo" & $i
Next

Thanks for the reply jguinch.  I have that working, but it isn't what I want.  I was hoping for a builtin function that loads multiple indexes directly without looping.  $arr[0..4] = "foo1" or some similar equivalent. 

Edited by Prefection
Link to comment
Share on other sites

Perfection,

There is nothing like the Excel range fill, if that is what you are after.  A loop similar to what jguinch posted is the easiest and best way to do this.

What is wrong with this...?

#include <array.au3>

local $aTest[3][28]

for $1 = 0 to UBound($aTest) - 1
    for $2 = 0 to ubound($aTest,2) - 1
        $aTest[$1][$2] = $1 & ' - ' & $2
    Next
Next

_arraydisplay($aTest)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators
  • Solution

You're never going to get out of "Looping", but there are always alternatives to handle your work.

They'll cost you speed sometimes, but sometimes that worth it making your code readable and easily maintainable.

Something like this maybe:

#include <Array.au3>

Global $gArr[3][31]

_myRangeFill($gArr, "foo6", "[0][10..27]")
_ArrayDisplay($gArr)

_myRangeFill($gArr, "foo5", "[0-2][1-9]", "-")
_ArrayDisplay($gArr)

Func _myRangeFill(ByRef $aArr, $vFill, $sIndices, $sSep = "..")

    If Not IsArray($aArr) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $aInd = StringRegExp($sIndices, "\[\s*(.+?)\s*\](?:\s*\[\s*(.+?)\s*\])?", 3)
    If @error Then
        ; improper indices sent
        Return SetError(2, 0, 0)
    EndIf

    Local $iUB = UBound($aInd)
    If Not UBound($aArr, $iUB) Then
        ; improper indices sent
        Return SetError(3, 0, 0)
    EndIf

    Local $aSp1, $aSp2
    Switch $iUB
        Case 1
            $aSp1 = StringSplit($aInd[0], $sSep, 1)
            If $aSp1[0] = 1 Then
                If (UBound($aArr, 1) - 1) < Int($aSp1[1]) Then
                    Return SetError(4, 0, 0) ; out of scope
                EndIf
                ReDim $aSp1[3]
                $aSp1[2] = $aSp1[1]
            EndIf
            For $i = Int($aSp1[1]) To Int($aSp1[2])
                $aArr[$i] = $vFill
            Next
        Case 2
            $aSp1 = StringSplit($aInd[0], $sSep, 1)
            If $aSp1[0] = 1 Then
                If (UBound($aArr, 1) - 1) < Int($aSp1[1]) Then
                    Return SetError(5, 0, 0) ; out of scope
                EndIf
                ReDim $aSp1[3]
                $aSp1[2] = $aSp1[1]
            EndIf
            $aSp2 = StringSplit($aInd[1], $sSep, 1)
            If $aSp2[0] = 1 Then
                If (UBound($aArr, 2) - 1) < Int($aSp2[1]) Then
                    Return SetError(6, 0, 0) ; out of scope
                EndIf
                ReDim $aSp2[3]
                $aSp2[2] = $aSp2[1]
            EndIf
            For $i = Int($aSp1[1]) To Int($aSp1[2])
                For $j = Int($aSp2[1]) To Int($aSp2[2])
                    $aArr[$i][$j] = $vFill
                Next
            Next
        Case Else
            ; improper indices sent (only doing 2D)
            Return SetError(7, 0, 0)
    EndSwitch

    Return 1
EndFunc

Edit:

After reading everyone elses code, I see I have probably misinterpreted what was wanted here.

I didn't know you wanted to enum the values.

Edited by SmOke_N

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.

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...