Jump to content



Photo

Add column to array


  • Please log in to reply
9 replies to this topic

#1 Odewallrus

Odewallrus

    Seeker

  • Active Members
  • 21 posts

Posted 24 November 2010 - 06:01 PM

Can someone please help me with why this code is not adding a column to the array? I have used it in the past, but cannot seem to figure out what I am missing here.

Plain Text         
$sINI = @ScriptDir & "\FileBackup.ini" $iSource = IniReadSection($sINI, "Source" Global $FileArray[1] $FileArray[0]="0" For $s = 1 To $iSource[0][0]     $iTypes = StringSplit($iSource[$s][1],",",0)     For $t = 1 To $iTypes[0]     $fArray = _FileListToArray_Recursive($iSource[$s][0], "", $iTypes[$t], "", 1, 2, True)     _ArrayConcatenate($FileArray,$fArray,1)     Next Next $aRows = UBound($FileArray) $FileArray[0] = $aRows - 1 _ArrayAdd_Column($FileArray) _ArrayDisplay($FileArray, "Debug: $FileArray") Func _ArrayAdd_Column($Array)     Local $aTemp[UBound($Array)][UBound($Array, 0) + 1]     For $i = 0 To UBound($Array) - 1                 For $j = 0 To UBound($Array, 0) - 1             If UBound($Array, 0) = 1 Then $aTemp[$i][0] = $Array[$i]             If UBound($Array, 0) > 1 Then $aTemp[$i][$j] = $Array[$i][$j]         Next     Next     Return $aTemp EndFunc  ;==>_ArrayAdd_Column








#2 Jos

Jos

    oh joy ...

  • Developers
  • 21,104 posts

Posted 24 November 2010 - 06:38 PM

$FileArray = _ArrayAdd_Column($FileArray)


Jos

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)


#3 Tvern

Tvern

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 972 posts

Posted 24 November 2010 - 06:39 PM

The function returns the modified array, but you never assign the return value to a variable. Like:
$FileArray = _ArrayAdd_Column($FileArray)

The better way would be to use ByRef though.
Also consider
ReDim $FileArray[UBound($FileArray)][UBound($FileArray,2)+1]


Edit: Captian slow to the rescue!

Edited by Tvern, 24 November 2010 - 06:39 PM.


#4 Odewallrus

Odewallrus

    Seeker

  • Active Members
  • 21 posts

Posted 24 November 2010 - 06:44 PM

The function returns the modified array, but you never assign the return value to a variable. Like:

$FileArray = _ArrayAdd_Column($FileArray)

The better way would be to use ByRef though.
Also consider
ReDim $FileArray[UBound($FileArray)][UBound($FileArray,2)+1]


Edit: Captian slow to the rescue!



THANK YOU!

#5 itwerx

itwerx

    Seeker

  • New Members
  • 1 posts

Posted 01 March 2011 - 11:41 PM

Redim loses the array's previous contents.
...and the _ArrayAdd_Column code above does not take into account the fact that Ubound returns 0 when there's only one column, but is accurate the rest of the time.

Here's a version of _ArrayAdd_Column which takes this into account:
Func _ArrayAdd_Column($Array)    if UBound($Array, 2) = 0 then                       ;Ubound returns 0 if there is only one column!       Local $aTemp[UBound($Array, 1)][2]                           ;...so force it manually the first time       $OrigColCount = 1    Else       Local $aTemp[UBound($Array, 1)][UBound($Array, 2) + 1]       ;...otherwise increment normally       $OrigColCount = UBound($Array, 2)    EndIf        For $r = 0 To UBound($Array, 1) - 1               For $c = 0 To $OrigColCount - 1          If $OrigColCount = 1 Then                                 ;And single column arrays don't like having a column index reference             $aTemp[$r][0] = $Array[$r]          Else             $aTemp[$r][$c] = $Array[$r][$c]          EndIf       Next    Next    Return $aTemp EndFunc


#6 MvGulik

MvGulik

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 2,795 posts

Posted 02 March 2011 - 03:30 AM

Redim ONLY loses the array's previous content IF the number of dimensions changes for that array.

Edited by iEvKI3gv9Wrkd41u, 02 March 2011 - 03:33 AM.


#7 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 7,068 posts

Posted 02 March 2011 - 03:32 AM

This function should work to do the trick.

AutoIt         
; #FUNCTION# ==================================================================================================================== ; Name...........: _ArrayAddColumns ; Description ...: Adds a specified number of columns to an array. ; Syntax.........: _ArrayAddColumns(ByRef $aArrayIn, $NumColCount) ; Parameters ....: $aArrayIn - Array to modify ;                  $NumColCount  - Number of columns to add (default = 1) ; Return values .: Success - New array with columns added ;                  Failure - -1, sets @error ;                  |1 - $aArrayIn is not an array ;                  |2 - $NumColCount is an invalid number ;                  |3 - Array has too many dimensions (2D array max) ; Author ........: Bob Marotte aka BrewManNH ; Remarks .......: This will add any number of columns to a 1D or 2D array of any size and preserves ;                  the contents of the array being modified ; Related .......: _ArrayConcatenate, _ArrayDelete, _ArrayInsert, _ArrayPop, _ArrayPush ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _ArrayAddColumns(ByRef $aArrayIn, $NumColCount = 1)     If Not IsArray($aArrayIn) Then         SetError(1)         Return -1     EndIf     If $NumColCount < 1 Then         SetError( 2)         Return -1     EndIf     Local $iDimensions = UBound($aArrayIn, 0)     If $iDimensions > 2 Then         SetError(3)         Return -1     EndIf     Local $NewArrayOut[UBound($aArrayIn)][$iDimensions + $NumColCount + 1]     For $I = 0 To UBound($aArrayIn) - 1         If $iDimensions > 1 Then             For $X = 0 To $iDimensions                 $NewArrayOut[$I][$X] = $aArrayIn[$I][$X]             Next         Else         $NewArrayOut[$I][0] = $aArrayIn[$I]         EndIf     Next     Return $NewArrayOut EndFunc   ;==>_ArrayAddColumns

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#8 MvGulik

MvGulik

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 2,795 posts

Posted 02 March 2011 - 04:32 AM

I don't really see the point of it ...
AutoIt         
Func _Array_ColumnAdjust($array, $add) ;; $add can be a positive or negative value. ;~  If 1 Then ;; parameter checkup. [debug] ;~      If Not IsArray($array) Then Exit 9931 ;; IsArray($array) <> 1 ;~      If Not (UBound($array, 0) = 1 Or UBound($array, 0) = 2) Then Exit 9932 ;~      If Not IsInt($add) Then Exit 9933 ;~  EndIf     If UBound($array, 2) And (UBound($array, 1) * UBound($array, 2) + $add) > Int(2 ^ 24) Then Exit 9939 ;; would cross array max-cel limit.     If Not UBound($array, 2) Then $array = _array_dim_1to2($array) ;; I would not do this ... not here at least.     If $add Then         If UBound($array, 2) + $add < 1 Then Exit 9938         ReDim $array[UBound($array, 1)][UBound($array, 2) + $add]     EndIf     If UBound($array, 2) = 1 Then $array = _array_dim_2to1($array) ;; I would not do this ... not here at least.     Return $array EndFunc Func _array_dim_1to2($array_in) ;~  If 1 Then ;; parameter checkup. [debug] ;~      If Not IsArray($array_in) Then Exit 9911 ;~      If Not (UBound($array_in, 0) = 1) Then Exit 9912 ;~  EndIf     If UBound($array_in, 1) * 2 > Int(2 ^ 24) Then Exit 9919 ;; would cross array max-cel limit.     Local $array_out[UBound($array_in, 1)][1]     For $i = 0 To UBound($array_in, 1) - 1         $array_out[$i][0] = $array_in[$i]     Next     Return $array_out EndFunc Func _array_dim_2to1($array_in) ;~  If 1 Then ;; parameter checkup. [debug] ;~      If Not IsArray($array_in) Then Exit 9921 ;~      If Not (UBound($array_in, 0) = 2) Then Exit 9922 ;~  EndIf     Local $array_out[UBound($array_in, 1)]     For $i = 0 To UBound($array_in, 1) - 1         $array_out[$i] = $array_in[$i][0]     Next     Return $array_out EndFunc

-3- other minor adjustments.

Edited by iEvKI3gv9Wrkd41u, 02 March 2011 - 12:10 PM.


#9 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 7,068 posts

Posted 02 March 2011 - 02:46 PM

If you send your function a variable that's not an array it crashes the script. If there are any other error conditions your function exits the script rather than returning an error code and continuing the script. Sending the function an $add parameter of 0 just returns the original script rather than telling the program that it didn't do anything, which might be an issue if you're using a variable that you set to 0 before calling the function, might cause troubleshooting issues in someone's script.

With my version I included error checking and not inadvertantly deleting columns that you didn't want to delete because in my function it is only adding columns and won't accept negative values. After all the function is called _ArrayAddColumns. Plus it returns from the function without killing the script. I wouldn't use an Exit command for errors, I'd use a Return statement and SetError

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#10 MvGulik

MvGulik

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 2,795 posts

Posted 03 March 2011 - 07:26 AM

As its intended as example code and its not claiming to be a fully finished UDF ... Its working perfectly in my view.
- Showing ReDim is maintaining the array data, when used correctly.
- Using Array[X][1] trick to simulate a one dimensional array with a two dimensional array.
- And how to check when a requested column change will trigger a max-array size violation. (can of course also be used for a similar AddRow function)
(I don't see any questions. So I leave it at that.)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users