Jump to content

extract a one-dimension array from a two-dimensions array


Zomp
 Share

Recommended Posts

Maybe an already discussed topic, but I apologize to not be able to search using the right keywords.

Suppose I have a two-dimensional array $MyArray2[...][...] and that I have to pass as a UDF parameter the array whose elements are $MyArray2[0][0],$MyArray2[0][1],...,$MyArray2[0][k] where k is the upper bound of the second dimension.

Which is the simplest way to do so?

Thanks.

Link to comment
Share on other sites

  • Moderators

Maybe an already discussed topic, but I apologize to not be able to search using the right keywords.

Suppose I have a two-dimensional array $MyArray2[...][...] and that I have to pass as a UDF parameter the array whose elements are $MyArray2[0][0],$MyArray2[0][1],...,$MyArray2[0][k] where k is the upper bound of the second dimension.

Which is the simplest way to do so?

Thanks.

Are you asking how to get the upper bound of the 2nd dimension? You're not quite clear on what you actually need there.

If so, UBound() can do it. UBound($MyArray, 2) - 1

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

Are you asking how to get the upper bound of the 2nd dimension? You're not quite clear on what you actually need there.

If so, UBound() can do it. UBound($MyArray, 2) - 1

No, I regret to have explained myself so poorly.

Suppose I have

$Array2[0][0]="Kosovo"

$Array2[0][1]=1999

$Array2[0][2]="Why yes?"

$Array2[1][0]="Ossetia"

$Array2[1][1]=2008

$Array2[1][2]="Why not?"

and

Func PrintRecord($myrecord)

consolewrite($myrecord[0] & "-" & $myrecord[1] & "-" & $myrecord[2] & @LF)

EndFunc

Is there any way to pass $Array2[1][0], $Array2[1][1], $Array2[1][2] as parameter $myrecord for Function PrintRecord?

Link to comment
Share on other sites

  • Moderators

No, I regret to have explained myself so poorly.

Suppose I have

$Array2[0][0]="Kosovo"

$Array2[0][1]=1999

$Array2[0][2]="Why yes?"

$Array2[1][0]="Ossetia"

$Array2[1][1]=2008

$Array2[1][2]="Why not?"

and

Func PrintRecord($myrecord)

consolewrite($myrecord[0] & "-" & $myrecord[1] & "-" & $myrecord[2] & @LF)

EndFunc

Is there any way to pass $Array2[1][0], $Array2[1][1], $Array2[1][2] as parameter $myrecord for Function PrintRecord?

It's a 2 D array, where index 1 and index 2 both have values... does it make any logical sense what so ever, that without making your "PrintRecord" function support both 2D and 1D arrays that it would be possible? How would it know what actual record you want.

You're still doing a poor job in explaining. What would $myrecord[0] etc... actually print out in the console from what you have provided?

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

I hope the following example will be clearer.

Suppose I have a 2 D array $NameAndPlace whose elements are (first index in rows, second index in columns):

0      1
0  Antony   Rome
1  Barbara   Bristol
2  Carl   London
[omissis]
26 Zelinda   Chicago

So, $NameAndPlace[2][1]="London".

Is there something similar to the following command?

$Section=$NameAndPlace[2][]

so to have

$Section[0]="Carl"
$Section[1]="London"

?

Link to comment
Share on other sites

  • Moderators

I hope the following example will be clearer.

Suppose I have a 2 D array $NameAndPlace whose elements are (first index in rows, second index in columns):

0      1
0  Antony   Rome
1  Barbara   Bristol
2  Carl   London
[omissis]
26 Zelinda   Chicago

So, $NameAndPlace[2][1]="London".

Is there something similar to the following command?

$Section=$NameAndPlace[2][]

so to have

$Section[0]="Carl"
$Section[1]="London"

?

Not without making something to do it for you as I already stated:
#include <array.au3>

Local $av_array[3][2] = [["Antony", "Rome"], ["Barbara", "Bristol"], ["Carl", "London"]]
_ArrayDisplay($av_array)
Local $a_converted = _myarrayconvert($av_array, 2)
_ArrayDisplay($a_converted)

Func _myarrayconvert($a_array, $i_row, $i_column = -1, $i_base = 0)
    If IsArray($a_array) = 0 Then Return SetError(1, 0, 0)
    
    Local $i_ub1 = UBound($a_array, 1), $i_ub2 = UBound($a_array, 2)
    If $i_ub1 - 1 < $i_row Then Return SetError(2, 0, 0)
    If $i_ub2 = 0 Then Return SetError(3, 0, 0)
    If $i_column <> -1 And $i_ub2 - 1 < $i_column Then Return SetError(4, 0, 0)
    If $i_column = -1 Then
        Local $a_ret[$i_ub2]
        For $i = $i_base To $i_ub2 - 1
            $a_ret[$i] = $a_array[$i_row][$i]
        Next
    Else
        Local $a_ret[1] = [$a_array[$i_row][$i_column]]
    EndIf
    Return $a_ret
EndFunc

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

No, I regret to have explained myself so poorly.

Suppose I have

$Array2[0][0]="Kosovo"

$Array2[0][1]=1999

$Array2[0][2]="Why yes?"

$Array2[1][0]="Ossetia"

$Array2[1][1]=2008

$Array2[1][2]="Why not?"

and

Func PrintRecord($myrecord)

consolewrite($myrecord[0] & "-" & $myrecord[1] & "-" & $myrecord[2] & @LF)

EndFunc

Is there any way to pass $Array2[1][0], $Array2[1][1], $Array2[1][2] as parameter $myrecord for Function PrintRecord?

And another example.

Global $Array2[2][3]

$Array2[0][0] = "Kosovo"
$Array2[0][1] = 1999
$Array2[0][2] = "Why yes?"

$Array2[1][0] = "Ossetia"
$Array2[1][1] = 2008
$Array2[1][2] = "Why not?"

PrintRecord(0)
PrintRecord(1)

$aArray = SingleArray(1)

MsgBox(0, "", "$aArray[0] = " & $aArray[0] & @CRLF & _
        "$aArray[1] = " & $aArray[1] & @CRLF & _
        "$aArray[2] = " & $aArray[2] & @CRLF)

;Returns a single dimension array based on $Array2 array
;$iIndex Parameter - index of 1st dimension of $Array2
Func SingleArray($iIndex)
    Local $aSingle[UBound($Array2, 2)]  ; This case will be 3
    
    For $x = 0 To UBound($Array2, 2) - 1   ;This case $x will be 0,1,2 only.
        $aSingle[$x] = $Array2[$iIndex][$x]
    Next
    Return $aSingle
EndFunc   ;==>SingleArray

;$iIndex Parameter - index of 1st dimension of $Array2
Func PrintRecord($iIndex)
    ConsoleWrite($Array2[$iIndex][0] & "-" & $Array2[$iIndex][1] & "-" & $Array2[$iIndex][2] & @LF)
EndFunc   ;==>PrintRecord
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...