Jump to content

2 dim array add and display functions


ivan
 Share

Recommended Posts

I tend to often use 2 dim arrays in my code and felt the dire need to add and display those arrays, pretty much similar (in fact extended from) _ArrayAdd and _ArrayDisplay.

; parameters:
;$2DimArray: your array
; $Index1: index of first dim where you want to add the value
; 2ndDimValue value to add in the second dim
Func ArrayAdd2Dim(ByRef $2DimArray, $Index1, $2ndDimValue)
   Local $Dim1 = Int(UBound($2DimArray, 1) - 1)
   Local $Dim2 = Int(UBound($2DimArray, 2) - 1)
   Local $LD1 = UBound($2DimArray, 1)
   Local $LD2 = Int(UBound($2DimArray, 2) + 1)
   Select
      Case $Index1 <> ""
         If IsArray($2DimArray) Then
            ReDim $2DimArray[$LD1][$LD2]
            $2DimArray[$Index1][$Dim2 + 1] = $2ndDimValue
            $2DimArray[0][0] = $Dim1
            $2DimArray[0][1] = $Dim2 + 1
            SetError(0)
            Return 1
         Else
            SetError(1)
            Return 0
         EndIf
      Case $Index1 == ""
         If IsArray($2DimArray) Then
            ReDim $2DimArray[$LD1][$LD2]
            $2DimArray[$Dim1][$Dim2 + 1] = $2ndDimValue
            $2DimArray[0][0] = $Dim1
            $2DimArray[0][1] = $Dim2 + 1
            SetError(0)
            Return 1
         Else
            SetError(1)
            Return 0
         EndIf
   EndSelect
EndFunc   ;==>ArrayAdd2Dim

; same principle as array add
Func ArrayDisplay2Dim(ByRef $2DimArray, $Title)
   Local $Dim1 = Int(UBound($2DimArray, 1) - 1)
   Local $Dim2 = Int(UBound($2DimArray, 2) - 1)
   Local $Str
   For $i = 1 To $Dim1 Step 1
      For $j = 0 To $Dim2 Step 1
         $Str = $Str & "[" & $i & "][" & $j & "]" &" = " &$2DimArray[$i][$j] & @CRLF
      Next
   Next
   MsgBox(0, $Title, $Str)
EndFunc   ;==>ArrayDisplay2Dim
oÝ÷ ØLZ^jëh×6
Dim $TwoDimArr[3][3]

$TwoDimArr[1][0] = "letters" 
$TwoDimArr[1][1] = "a" 
$TwoDimArr[1][2] = "b" 
$TwoDimArr[2][0] = "numbers" 
$TwoDimArr[2][1] = "1" 
$TwoDimArr[2][2] = "2" 

ArrayDisplay2Dim($TwoDimArr, '$Title')
ArrayAdd2Dim($TwoDimArr, 1, 'intrusion')
ArrayDisplay2Dim($TwoDimArr, '$Title')


#region 2DIM ARRAYS
Func ArrayAdd2Dim(ByRef $2DimArray, $Index1, $2ndDimValue)
   Local $Dim1 = Int(UBound($2DimArray, 1) - 1)
   Local $Dim2 = Int(UBound($2DimArray, 2) - 1)
   Local $LD1 = UBound($2DimArray, 1)
   Local $LD2 = Int(UBound($2DimArray, 2) + 1)
   Select
      Case $Index1 <> ""
         If IsArray($2DimArray) Then
            ReDim $2DimArray[$LD1][$LD2]
            $2DimArray[$Index1][$Dim2 + 1] = $2ndDimValue
            $2DimArray[0][0] = $Dim1
            $2DimArray[0][1] = $Dim2 + 1
            SetError(0)
            Return 1
         Else
            SetError(1)
            Return 0
         EndIf
      Case $Index1 == ""
         If IsArray($2DimArray) Then
            ReDim $2DimArray[$LD1][$LD2]
            $2DimArray[$Dim1][$Dim2 + 1] = $2ndDimValue
            $2DimArray[0][0] = $Dim1
            $2DimArray[0][1] = $Dim2 + 1
            SetError(0)
            Return 1
         Else
            SetError(1)
            Return 0
         EndIf
   EndSelect
EndFunc   ;==>ArrayAdd2Dim

Func ArrayDisplay2Dim(ByRef $2DimArray, $Title)
   Local $Dim1 = Int(UBound($2DimArray, 1) - 1)
   Local $Dim2 = Int(UBound($2DimArray, 2) - 1)
   Local $Str
   For $i = 1 To $Dim1 Step 1
      For $j = 0 To $Dim2 Step 1
         $Str = $Str & "[" & $i & "][" & $j & "]" &"=" &$2DimArray[$i][$j] & @CRLF
      Next
   Next
   MsgBox(0, $Title, $Str)
EndFunc   ;==>ArrayDisplay2Dim
Link to comment
Share on other sites

I like the functions ivan. I also use 2D Arrays a lot.

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Hi,

I used DannyD's I think, and others to make "Array2D.au3" in my signature; there are also functions to replace rows or columns with arrays or delimited strings;as well as 2Dsort, subsort, transpose, etc

Here is your example using that;

;2DExample2.au3 _3
#include "Array2D.au3" ;
Dim $OneDimArr[3]
$OneDimArr[0] = "||" 
$OneDimArr[1] = "letters|a|b" 
$OneDimArr[2] = "numbers|1|2" 
$TwoDimArr=_Array2DCreateFromArraySt($OneDimArr,1)
_ArrayViewText($TwoDimArr, "2D Display")
$aRowString='intrusionROW'
_ArrayInsert2Dst($TwoDimArr, $aRowString)
_ArrayViewText($TwoDimArr,"After Insert/ Add ")
$aColString='|intrusionCOLUMN'
_ArrayInsert2DColumnSt( $TwoDimArr,$aColString)
_ArrayViewText($TwoDimArr,"After Insert/ Add ")
Best, randall

PS I say this to encourage you to develop this, as I have no error checking etc; needs well-written funcs

Edited by randallc
Link to comment
Share on other sites

Hi,

- questions please :

1]

1D, 2D arrays exist in AutoIT, but 3D, 4d, nD also ?

2]

dynamic autoadjustable arrays exist in AutoIt without need if ReDimming them ?

Yours,(¯`·._.·[Gerome GUILLEMIN] [Freestyle Basic Script Language Author]·._.·´¯):: Full SETUP w. HELP ::FBSL official web siteA keyboard is infinitely better as a programming tool than a toaster...

Link to comment
Share on other sites

Sorry everyone for not replying promptly, the boss is after kicking my but for being lazy... And today is my daughter's 2nd birthday, so I'm gonna be short here, as I've still to buy the present (a tricycle).

There's a couple of other functions in the oven. The first is adding one element to dim 1, the second is a search for specific txt. I'll post those here once I get them done.

@RazerM:

Thanks for the comment.

@gafrost:

I remember having posted something similar yonks ago, but it was incomplete, no examples either, no documentation whatsoever, and I sincerely can't even remember whether I posted here or in the support forum. This, besides making me lazy also makes me disorganized. Also I've just read randallc's comment, so it was probably him as he's got a much more developed UDF than this snippet.

@randallc:

Just downloaded your UDF. I'll check it out tomorrow. Thansk for the encouragement, it's part of what keeps everyone here going.

@Gerome: You gave a thought for adding to dim1

@nfwu: Thanks for supporting here.

IVAN

Edited by ivan
Link to comment
Share on other sites

Hi,

I just come here by curiosity, and i respect the AutoIt development, even if i don't develop/use at all using autoIt, just come to see and asking to give me ideas, to compare ideas and algos, but nothing offensive :whistle:

Yours,(¯`·._.·[Gerome GUILLEMIN] [Freestyle Basic Script Language Author]·._.·´¯):: Full SETUP w. HELP ::FBSL official web siteA keyboard is infinitely better as a programming tool than a toaster...

Link to comment
Share on other sites

@Gerome:

You'll find plenty to fulfill most your interests around here. Autoit V3 has exploded into just that, take a look at what was acheived by contributors in the UDFs in the help file of the latest beta and you'll see what I'm talking about, let alone all those great developments that have not become part it, yet.

For me the questions stands at what cannot be done with it.

There's also quite a few french speaking members.

As they say, au revoir.

IVAN

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