Sign in to follow this  
Followers 0
klsgfx

2-dim. arrays - aargh

6 posts in this topic

Hello moim,

is there anybody out there, who can help me with this problem?

I try to build an 2-dimensional array in two different ways, but obviously they are not aquivalent.

What is the difference? Which one is correct and why the other not?

$strTypes = ('*.jpg/-;*.nef/-;*.avi/*.thm')
$arrPictures = StringSplit($strTypes, ';')
_ArrayDelete($arrPictures, 0)
For $i = 0 To UBound($arrPictures) -1
    $arrTypes = StringSplit($arrPictures[$i], '/')
    _ArrayDelete($arrTypes, 0)
    $arrPictures[$i] = $arrTypes
Next
_ArrayDisplay($arrPictures, 'Elements') ; <-- Top1
For $i = 0 To UBound($arrPictures)-1
    _ArrayDisplay($arrPictures[$i], 'Element ' & $i) ; <-- Top2
    ConsoleWrite('--> ' & $arrPictures[$i][0] & ' : ' & $arrPictures[$i][1] & @LF) ; <-- Top3
NextoÝ÷ Ù«­¢+Ù1½°ÀÌØíÉÉ¥lÍulÉtômlÌä쨹©ÁÌäì°Ìäì´Ìäít°lÌä쨹¹Ìäì°Ìäì´Ìäít°lÌä쨹٤Ìäì°Ìä쨹ѡ´Ìäíut)}ÉÉå¥ÍÁ±ä ÀÌØíÉÉ¥°Ìäí±µ¹ÑÌÌäì¤ì±Ðì´´Q½ÀÐ)½ÈÀÌØíàôÀQ¼U   ½Õ¹ ÀÌØíÉÉ¥¤´Ä(}ÉÉå¥ÍÁ±ä ÀÌØíÉÉ¥lÀÌØíát°Ìäí±µ¹ÐÌäìµÀìÀÌØíà¤ì±Ðì´´Q½ÀÔ(
½¹Í½±]É¥Ñ Ìäì´´ØÈìÌäìµÀìÀÌØíÉÉ¥lÀÌØíáulÁtµÀìÌäìèÌäìµÀìÀÌØíÉÉ¥lÀÌØíáulÅtµÀì1¤ì±Ðì´´Q½ÀØ)9á

As you can see if you try, _ArrayDisplay at Top1 (example 1) and Top4 (example 2) shows different results, shouldn't it show the same?

Furthermore: Top2 is working, while Top3 not and in the second example Top5 is not working, but Top6

Where am I wrong?

Cheers

Klaus

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

In the first block of code, your arrays are all 1D arrays. Where is it you expect a 2D array to come from?

In the second block of code, you correctly declared a 2D, but tried to make a 1D reference to it at "top5". In addition, you are attempting to pass an element of an array to _ArrayDisplay() instead to the array itself, which is invalid unless that element is a nested array (which I don't think you are getting into).

:)

P.S. On looking again, perhaps that is your problem, you are trying to treat a 2D array as a nested array. They are not the same thing and a 2D array reference will not show you data in a nested array, nor will storing a 1D array into an element of another 1D array cause it to become a 2D array.

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

On looking again, perhaps that is your problem, you are trying to treat a 2D array as a nested array. They are not the same thing and a 2D array reference will not show you data in a nested array, nor will storing a 1D array into an element of another 1D array cause it to become a 2D array.

Ok, seems like I do not know the difference between a 2D array and a nested array. :)

I thought that was the same.

Ok, I am out and away heading for wikipedia.

Thanks PsaltyDS.

Ah, btw, how can I convert this

$strTypes = ('*.jpg/-;*.nef/-;*.avi/*.thm')
into an 2D array?

Cheers

Klaus

Share this post


Link to post
Share on other sites

Ok, seems like I do not know the difference between a 2D array and a nested array. :)

I thought that was the same.

Ok, I am out and away heading for wikipedia.

Thanks PsaltyDS.

Ah, btw, how can I convert this

$strTypes = ('*.jpg/-;*.nef/-;*.avi/*.thm')oÝ÷ Ú)í¡©ö
ªëk ¡yêì*V®³ú®¢×*.z0~캷­êÞÂ)emëp¢«¨·ú®¢×^)¢µçbµé¬wr®¢Ú®¢×^)¢µçlyËb¢{az{-®)à²b¶+m¡­CjºÚÊØ^ÊeÛaz«¶È§jz-êÚ®¶²jwZ²Ç¦nW­êÞªè«yÚ.¶­jëh×6#include <array.au3> ; Only for _ArrayDisplay()

Global $strTypes = ('*.jpg/-;*.nef/-;*.avi/*.thm'), $avTemp1[1][1], $avTemp2[1]
Global $avTypes = StringSplit($strTypes, ";") ; Makes a 1D array
If $avTypes[0] > 0 Then
    ReDim $avTemp1[$avTypes[0] + 1][2] ; Makes a 2D array
    $avTemp1[0][0] = $avTypes[0] ; Preserve the count
    For $n = 1 To $avTypes[0]
        $avTemp2 = StringSplit($avTypes[$n], "/") ; Makes a 1D array
        If $avTemp2[0] = 2 Then
            $avTemp1[$n][0] = $avTemp2[1]
            $avTemp1[$n][1] = $avTemp2[2]
        Else
            MsgBox(16, "Error", "Invalid data in $avTypes[" & $n & "][0] = " & $avTypes[$n][0])
        EndIf
    Next
    $avTypes = $avTemp1
Else
    MsgBox(16, "Error", "Invalid $strTypes = " & $strTypes)
EndIf

_ArrayDisplay($avTypes, "Debug: $avTypes, Final")

I prefer to keep the count in [0] for a 1D, or [0][0] for a 2D array, but you could remove that if you don't want it.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I prefer to keep the count in [0] for a 1D, or [0][0] for a 2D array, but you could remove that if you don't want it.

:)

No that's fine.

Thanks very much.

Looking at your code I get the feeling, that I did not put enough effort into this, I should have been able to write this by my self - sigh :P

[fool]I still don't get it - is there a special reason for having a difference between 2D-arrays and nested arrays?[/fool]

Cheers

Klaus

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

[fool]I still don't get it - is there a special reason for having a difference between 2D-arrays and nested arrays?[/fool]Klaus

Hi,

Unfortunately, or not that is just the way AutoIt Arrays have been written for maximum speed etc, and like vbscript; see "ArrayProb" link in my sig which points to some more help.

It is easy enoough to write a UDF which works as you would be used to; example attached, although this one is much more complicated than it needs to be as I was exploring having an array of objects, each of which held an array in a scripting object; I am now re-writing it to use simple 2D array with syntax people coming from other languages might find familiar.

Best, Randall

; Arraytype.au3
#include<array.au3>
#include<DirectSubArrays.au3>
Local $strTypes = ('*.jpg/-;*.nef/-;*.avi/*.thm')
Local $sNames = ('jpg;nef;avi_thm')
$arrPicturesTmp = StringSplit($strTypes, ';')
$arrNames = StringSplit($sNames, ';')
Local $arrPictures1D[UBound($arrPicturesTmp) - 1 ]
If UBound($arrPicturesTmp) > UBound($arrNames) Then ReDim $arrNames[ UBound($arrPicturesTmp) ]
For $i = 0 To UBound($arrPicturesTmp) - 2
    $arrTypes = StringSplit($arrPicturesTmp[$i + 1], '/')
    _arAddArray ($arrPictures1D[$i ], $arrTypes, $arrNames[$i + 1])
Next
;~ For $i = 0 To UBound($arrPictures1D)-1
;~  _ArrayDisplay(_arGetArray1D ($arrPictures1D[$i]), "_arGetArray1D($arrPictures1D["&$i&"])")
;~ Next
$arArray2D = _arCreate2D ($arrPictures1D)
_ArrayDisplay($arArray2D, "$arArray2D")
Edited by randallc

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