Jump to content
satanico64

Problem with _arraytranspose

Recommended Posts

satanico64

hi guys ! nice to see you again !

i've just spend hours on the forum, until it went down... : \

so forum is back and me too, and here's the problem

Problem with array :
i've a 2d array, from which i want to extract 1 row with all it's value , to get a 1d array.

Method:

1 i search my value => got $index in array
2 i _arrayextract 1 row > But it still make a 2d array (1 value by column)
3 i arraytranspose => And it should make 1 d array

but not.... let's have a look with simplified code

Thanks to explain where i am wrong ?

ps: I've just updated latest stable version of autoit because i had bug with extract column zero . it's ok now but my transpose does'nt seems to work anymore...

thanks

nicolas.
Simple code:

#include <Array.au3>

Local $aArray[4][4]
For $i = 0 To 3
    For $j = 0 To 3
        $aArray[$i][$j] = $i & $j
    Next
Next
_ArrayDisplay($aArray, "Original")   ; >Display a 2d array

Local $aExtract = _ArrayExtract($aArray, 0, 0)   ; for me it extract 1 row but still 2d array
_ArrayDisplay($aExtract, "Row 0-0")
ConsoleWrite('dimension:' & UBound($aExtract, $UBOUND_DIMENSIONS ) & @CRLF )  ; It's a 2d array
_ArrayTranspose($aExtract)   ; I thought i should obtain a 1d array with that
_ArrayDisplay($aExtract, "Row 0-0")
ConsoleWrite('dimension:' & UBound($aExtract, $UBOUND_DIMENSIONS ) & @CRLF )  ; It's a 2d array

 

Share this post


Link to post
Share on other sites
water

_ArrayTranspose only swaps rows and columns but it doesn't change the number of dimensions. Seems you have to code it yourself.
But this is no brain surgery.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Example:

#include <Array.au3>
Global $aArray2D[3][3] = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
_ArrayDisplay($aArray2D)
Global $aArray1D[1]
Global $iIndex2D = 1
ReDim $aArray1D[UBound($aArray2D, 2)]
For $i = 0 to UBound($aArray2D, 2) - 1
    $aArray1D[$i] = $aArray2D[$iIndex2D][$i]
Next
_ArrayDisplay($aArray1D)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
satanico64

hmm i humbly disagree (or i simply don't understand), If you take a 1D array with more than 1 record, and you swap it make a 2d array.

If you swap again it should make a 1D array. (in our case where it has only one row)

 

i take the help file from _ArrayTranspose, it indicate that it transpose array from 1d to 2d, and 2d to 1d

the example provided, i just add consolewrite dimensions and remove first example:

 

#include <Array.au3>

Local $aArray[5] = [1, 2, 3, 4, 5]
ConsoleWrite('Initial 1D dimension :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Original 1D")
_ArrayTranspose($aArray)
ConsoleWrite('dimension after transpose of 1D array :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Transposed to 2D")
_ArrayTranspose($aArray)
ConsoleWrite('dimension after transpose of 2D array :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Re-transposed to 1D")   ;>>>> BUT IT FAILED HERE, still 2D

 

where am i wrong ?

ps: Thanks Water for taking a look, i'm honored, your work helped me a lot, many times.

Share this post


Link to post
Share on other sites
water

As I see it the example is misleading/wrong. The UDF is not able to tell if a 2D array with only a single line should be a 1D array or a 2D array with only one column after transposing.
Will discuss this with the Devs.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
satanico64

i've just install old autoit version (3.3.12.0) on other computer, and the same last script transpose from 2d to 1d

 

Nicolas.

Share this post


Link to post
Share on other sites
satanico64

by the way, for my original problem another solution is:

1 find index

2 transpose => still 2D but i 'll extract 1 column and not a row

3 extract one column => 1d array.

 

Edited by satanico64
missed one character

Share this post


Link to post
Share on other sites
water

i've just install old autoit version (3.3.12.0) on other computer, and the same last script transpose from 2d to 1d

Thanks for that info.
Already passed to the Devs.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Melba23 has already created this modification. There is a new parameter to specify if you want to get a 1D or 2D array in this special case.
Could you please check and post your findings?

#include <Array.au3>

Local $aArray[5] = [1, 2, 3, 4, 5]
_ArrayDisplay($aArray, "Original 1D")
_ArrayTranspose_Mod($aArray)
_ArrayDisplay($aArray, "Transposed to 2D")
_ArrayTranspose_Mod($aArray)
_ArrayDisplay($aArray, "Re-transposed as 2D")
_ArrayTranspose_Mod($aArray)
_ArrayDisplay($aArray, "Still 2D")
_ArrayTranspose_Mod($aArray, True) ; Force 1D return
_ArrayDisplay($aArray, "Re-transposed to 1D")

Func _ArrayTranspose_Mod(ByRef $aArray, $bForce1D = False)
    Switch UBound($aArray, 0)
        Case 0
            Return SetError(2, 0, 0)
        Case 1
            Local $aTemp[1][UBound($aArray)]
            For $i = 0 To UBound($aArray) - 1
                $aTemp[0][$i] = $aArray[$i]
            Next
            $aArray = $aTemp
        Case 2
            Local $iDim_1 = UBound($aArray, 1), $iDim_2 = UBound($aArray, 2)
            If $iDim_1 <> $iDim_2 Then
                If $iDim_1 = 1 And $bForce1D Then ; If only 1 row and 1D return forced
                    Local $aTemp[$iDim_2]
                    For $i = 0 To $iDim_2 - 1
                        $aTemp[$i] = $aArray[0][$i]
                    Next
                Else ; Stay as 2D
                    Local $aTemp[$iDim_2][$iDim_1]
                    For $i = 0 To $iDim_1 - 1
                        For $j = 0 To $iDim_2 - 1
                            $aTemp[$j][$i] = $aArray[$i][$j]
                        Next
                    Next
                EndIf
                $aArray = $aTemp
            Else ; optimimal method for a square grid
                Local $vElement
                For $i = 0 To $iDim_1 - 1
                    For $j = $i + 1 To $iDim_2 - 1
                        $vElement = $aArray[$i][$j]
                        $aArray[$i][$j] = $aArray[$j][$i]
                        $aArray[$j][$i] = $vElement
                    Next
                Next
            EndIf
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Return 1
EndFunc   ;==>_ArrayTranspose

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
satanico64

It works like a charm.

Thanks all of you.

And I wish you a happy new year from Bordeaux, France.

Nicolas.

my test code:

#include <Array.au3>

Local $aArray[5] = [1, 2, 3, 4, 5]
_ArrayDisplay($aArray, "Original 1D")
ConsoleWrite('Initial 1D dimension :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )

_ArrayTranspose_Mod($aArray)
ConsoleWrite('Dimension Transposed to 2D :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Transposed to 2D")

_ArrayTranspose_Mod($aArray)   ; still 2D, but expected.
ConsoleWrite('re transpose to initial without force, dimension :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Re-transposed as initial without force ")

_ArrayTranspose_Mod($aArray)
ConsoleWrite('re Transposed to 2D :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF ) ;obvsiouly 2D
_ArrayDisplay($aArray, "Transposed to 2D again")

_ArrayTranspose_Mod($aArray, True) ; Force 1D return  => Now 1D. Hoped. Obtained. Impressed.
ConsoleWrite('Transposed to 1d with force :' & UBound($aArray, $UBOUND_DIMENSIONS ) & @CRLF )
_ArrayDisplay($aArray, "Transposed to initial")


Func _ArrayTranspose_Mod(ByRef $aArray, $bForce1D = False)
    Switch UBound($aArray, 0)
        Case 0
            Return SetError(2, 0, 0)
        Case 1
            Local $aTemp[1][UBound($aArray)]
            For $i = 0 To UBound($aArray) - 1
                $aTemp[0][$i] = $aArray[$i]
            Next
            $aArray = $aTemp
        Case 2
            Local $iDim_1 = UBound($aArray, 1), $iDim_2 = UBound($aArray, 2)
            If $iDim_1 <> $iDim_2 Then
                If $iDim_1 = 1 And $bForce1D Then ; If only 1 row and 1D return forced
                    Local $aTemp[$iDim_2]
                    For $i = 0 To $iDim_2 - 1
                        $aTemp[$i] = $aArray[0][$i]
                    Next
                Else ; Stay as 2D
                    Local $aTemp[$iDim_2][$iDim_1]
                    For $i = 0 To $iDim_1 - 1
                        For $j = 0 To $iDim_2 - 1
                            $aTemp[$j][$i] = $aArray[$i][$j]
                        Next
                    Next
                EndIf
                $aArray = $aTemp
            Else ; optimimal method for a square grid
                Local $vElement
                For $i = 0 To $iDim_1 - 1
                    For $j = $i + 1 To $iDim_2 - 1
                        $vElement = $aArray[$i][$j]
                        $aArray[$i][$j] = $aArray[$j][$i]
                        $aArray[$j][$i] = $vElement
                    Next
                Next
            EndIf
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Return 1
EndFunc   ;==>_ArrayTranspose

 

Share this post


Link to post
Share on other sites
water

Thanks for testing.
I'm glad this little modification solved your problem.
Will tell Melba about the result so he might include it in the Array UDF.

Happy new year from Lake Constance!
 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Dzenan03
      By Dzenan03
      I want to make a while loop, that creates variables based on a array. For thist I created the array $iDsO with the number and the name of folders in an other folder. Every folder has a different name an I want to create variables(arrays) for each folder that show me all the files in that folder. For example: I have the Folder \Folder1. In it there are the Folders \1, \2, \3. In 1, 2 and 3 there are some files(.png). The array for Folder1 is $iDsO and now I want to crate the arrays $iDsO1, $iDsO2 and $iDsO3 with the files in them can I make something like this:
      While $iDs > 0 ;$iDs is the number of files in Folder1>> $iDsO[0] $iDs#here should come the Foldername for example '1'# = _FileListtoArray(@ProgramFilesDir&"\Folder1\"&$iDsO[$iDs]) $iDs = $iDs - 1 Wend So that in the End I have three variabels ($iDs1, $iDs2 and $iDs3)
       
      Is this posible or if not what could I do instead ( I don´t know the number of folders in Folder1 in the begining).
×