Jump to content
Sign in to follow this  
Hobbyist

Changes In Array

Recommended Posts

Hobbyist

Would somebody take a look at my attempt to make changes within an array and let me know if it is a best approach, slow approach or anything constructive as I am totally new at this stuff.

Essentially I am trying to reassign array values to different columns.  So for example I might have an item in column 0 that I want in column 2.

The files being read are csv (three values per line) and I am reading them into an array, hence they show up in columns 0,1,and 2.  The input files have to remain item1, item2, item3 so there is no altering the actual input files. The array is 2D (n rows and 6 columns(0 - 5)). All error checking for values is done at the input stage. The values are:

item1 = string

item2 = string

item3 = number

My intial array in columns 0 -2: string, string, number.

I'm looking to rearrange the order and columns.

So column 2 gets the string from column 0, column 4 gets the number value from column 1 and column 5 gets the string from column 2.

So here is what I did. It works but is it a best solution approach??

Thanks.

Sorry I just ran it and it does NOT work. So yes, I need help for sure.

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>


$csv = FileRead ("C:\documents\abc.csv") 
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aLog[$rows[0] + 1][6]
$aLog[2][0] = $rows[0]  
For $i = 1 to $rows[0] 

    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1

        $aLog[$i][$j] = $temp[$j]
if $aLog[$i][5] = "" Then
$aLog[$i][5] = $aLog[$i][2]
EndIf
if $aLog[$i][4] = "" Then
$aLog[$i][4] = $aLog[$i][1]
EndIf
if $aLog[$i][2] <> "" Then
$aLog[$i][2] = ""
EndIf
if $aLog[$i][1] <> "" Then
$aLog[$i][1] = ""
EndIf
if $aLog[$i][1] = "" Then
$aLog[$i][2] = $aLog[$i][0]
EndIf

    Next

   Next
For $i = 0 To UBound($aLog,1)-1
 for $j = 0 to UBound($aLog,2) -1
    if string($aLog [$i] [0]) Then
$aLog[$i] [0] = ""
EndIf
 Next
Next
 

 _ArrayDisplay($aLog, "Montly log ")
Edited by Hobbyist

Share this post


Link to post
Share on other sites
computergroove

1,11,One
2,22,Two
3,33,Three
4,44,Four
5,55,Five
6,66,Six

Does this look like your text file?

*Edit 1

Can you please translate what this is supposed to do?

Dim $aLog[$rows[0] + 1][6]




			
				


	Edited  by computergroove
	
	

			
		

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
Luigi

Please, charge/load/post 'C:documentsabc.csv'


Visit my repository

Share this post


Link to post
Share on other sites
computergroove

I made one up and it seems to be working although he mentioned issues and I am seeing some in the final _arraydisplay output


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
computergroove

 

Would somebody take a look at my attempt to make changes within an array and let me know if it is a best approach, slow approach or anything constructive as I am totally new at this stuff.

Essentially I am trying to reassign array values to different columns.  So for example I might have an item in column 0 that I want in column 2.

The files being read are csv (three values per line) and I am reading them into an array, hence they show up in columns 0,1,and 2.  The input files have to remain item1, item2, item3 so there is no altering the actual input files. The array is 2D (n rows and 6 columns(0 - 5)). All error checking for values is done at the input stage. The values are:

item1 = string

item2 = string

item3 = number

My intial array in columns 0 -2: string, string, number.

I'm looking to rearrange the order and columns.

So column 2 gets the string from column 0, column 4 gets the number value from column 1 and column 5 gets the string from column 2.

So here is what I did. It works but is it a best solution approach??

Thanks.

Sorry I just ran it and it does NOT work. So yes, I need help for sure.

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>


$csv = FileRead ("C:\documents\abc.csv") 
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aLog[$rows[0] + 1][6]
$aLog[2][0] = $rows[0]  
For $i = 1 to $rows[0] 

    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1

        $aLog[$i][$j] = $temp[$j]
if $aLog[$i][5] = "" Then
$aLog[$i][5] = $aLog[$i][2]
EndIf
if $aLog[$i][4] = "" Then
$aLog[$i][4] = $aLog[$i][1]
EndIf
if $aLog[$i][2] <> "" Then
$aLog[$i][2] = ""
EndIf
if $aLog[$i][1] <> "" Then
$aLog[$i][1] = ""
EndIf
if $aLog[$i][1] = "" Then
$aLog[$i][2] = $aLog[$i][0]
EndIf

    Next

   Next
For $i = 0 To UBound($aLog,1)-1
 for $j = 0 to UBound($aLog,2) -1
    if string($aLog [$i] [0]) Then
$aLog[$i] [0] = ""
EndIf
 Next
Next
 

 _ArrayDisplay($aLog, "Montly log ")

I only a little about arrays and I am trying to tackle this. The online documentation wiki for arrays doesn't cover in any depth manipulating a 2d array. I want to figure this out and I have added some lines after every change to try and understand how you came up with your for loop. Did you write this? Would you mind commenting your source so I can better understand what the thought process was for this? I want to make a tutorial to help people better understand 2d arrays in autoit and this is a great example to have someone try.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
ZacUSNYR

My question is, why do you want to "flip" a column?  What does it matter?  Just write it out the way you'd want it to be outputted?

But it's not too complicated, just make a throw away array, size it the same as the current array.  Replace the columns on the output to the other array.

Example.

#include <Array.au3>
Global $asTestArray[5][10]
 
__Array2DFillWCrap($asTestArray)
_ArrayDisplay($asTestArray, "Starting Array")
__Array2DColFlip($asTestArray, 8, 1)
_ArrayDisplay($asTestArray, "Array Columns 8 and 1 Flipped")
__Array2DColFlip($asTestArray, 2, 9)
_ArrayDisplay($asTestArray, "Array Columns 2 and 9 Flipped")
 
Func __Array2DColFlip(ByRef $avArray, $iColA, $iColB)
Local $avTempArray[UBound($avArray)][UBound($avArray, 2)]
For $i = 0 To UBound($avArray) - 1
For $n = 0 To UBound($avArray, 2) - 1
If $n = $iColA Then
$avTempArray[$i][$iColB] = $avArray[$i][$iColA]
ElseIf $n = $iColB Then
$avTempArray[$i][$iColA] = $avArray[$i][$iColB]
Else
$avTempArray[$i][$n] = $avArray[$i][$n]
EndIf
Next
Next
$avArray = $avTempArray
Return
EndFunc
 
Func __Array2DFillWCrap(ByRef $avArray)
For $i = 0 To UBound($avArray) - 1
For $n = 0 To UBound($avArray, 2) - 1
$avArray[$i][$n] = "Orig Col : " & $n
Next
Next
EndFunc

Share this post


Link to post
Share on other sites
computergroove

This is what I came up with:

csv file:

1,11,one
2,22,two
3,33,three
4,44,four
5,55,five
6,66,six
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>


$csv = FileRead ("C:\documents\abc.csv")
$csv = StringStripWS($csv, 7);removes white spaces from data
$rows = StringSplit($csv, @CRLF);Converts the data to an array
Local $aLog[$rows[0] + 1][6];$rows[0] reads the number of rows and subtracts 1 to make 6 rows and then 6 columns
;$aLog[2][0] = $rows[0];This line doesn't change the output
For $i = 1 to $rows[0]

    $temp = StringSplit($rows[$i], ",", 2);Creates a 1D array with the data seperated by the delimiter from the first line of the csv file
    $temp1 = $temp[0]
    $temp2 = $temp[1]
    $temp3 = $temp[2]
    $aLog[$i][2] = $temp1
    $aLog[$i][4] = $temp2
    $aLog[$i][5] = $temp3
    ;msgbox(0,"",$temp1 & " " & $temp2 & " " & $temp3)
Next
    _ArrayDisplay($aLog)

Run it and look at the output. Is this what you want to do?

Keywords: 2 Dimensional Array Autoit 2d Multidimensional manipulating writing reading

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
mikell

Zac's solution seems nice, can be easily adapted to swap more cols

It could be done a bit faster though

#include <Array.au3>
Global $asTestArray[5][10]
 
__Array2DFillWCrap($asTestArray)
_ArrayDisplay($asTestArray, "Starting Array")
__Array2DColFlip($asTestArray, 8, 5, 1)
_ArrayDisplay($asTestArray, "Array Columns 8 , 5 and 1 Flipped")
 
Func __Array2DColFlip(ByRef $avArray, $iColA, $iColB, $iColC)
  Local $avTemp[UBound($avArray)]
  For $i = 0 To UBound($avArray) - 1
        $avTemp[$i] = $avArray[$i][$iColA]
        $avArray[$i][$iColA] = $avArray[$i][$iColB]
        $avArray[$i][$iColB] = $avArray[$i][$iColC]
        $avArray[$i][$iColC] = $avTemp[$i]
  Next
EndFunc
 
Func __Array2DFillWCrap(ByRef $avArray)
 For $i = 0 To UBound($avArray) - 1
    For $n = 0 To UBound($avArray, 2) - 1
         $avArray[$i][$n] = "Row " & $i & " Col " & $n
    Next
 Next
EndFunc

:)

  • Like 1

Share this post


Link to post
Share on other sites
ZacUSNYR

Yea that makes more sense then doing it per column heh

Edited by ZacUSNYR

Share this post


Link to post
Share on other sites
Hobbyist

Hey thanks!

Being new at this I need to get an idea of the logic approach more experienced people use.  

I copied/pasted the solutions and tried 'em out.

Zac's/Mikell works fine and I'm going through them to better understand the how/why.

Computergroove - I used your data and code but got an error message back on the subscripts.

It would help me if you could comment on the logic or logic flaws in my attempt. Its a good way for me to learn.

Thanks again.

Share this post


Link to post
Share on other sites
mikell

For your particular need the most simple and easy way is by far this one

#include <Array.au3>
#include <File.au3>

#cs
Global $avArray
$csv = "1.csv"    ; csv file
_FileReadToArray($csv, $avArray, 0, ",")
#ce

Global $avArray[4][3] = [ _
["string1-1", "string2-1", 1], _
["string1-2", "string2-2", 2], _
["string1-3", "string2-3", 3], _
["string1-4", "string2-4", 4] ]


_ArrayDisplay($avArray, "$avArray")

Redim $avArray[UBound($avArray)][6]
For $i = 0 To UBound($avArray) - 1   ; loop rows
    $avArray[$i][5] = $avArray[$i][2]   ; copy col 2 to col 5
    $avArray[$i][2] = $avArray[$i][0]   ; copy col 0 to col 2
    $avArray[$i][4] = $avArray[$i][1]   ; copy col 1 to col 4
    $avArray[$i][0] = ""   ; empty col 0
    $avArray[$i][1] = ""   ; empty col 1
Next

_ArrayDisplay($avArray, "$avArray")

Edit

Assuming the csv is properly structured (neither blank nor incomplete lines) you could try this

:)

#include <Array.au3>
#include <File.au3>

$txt = "string1-1,string2-1,1" & @crlf & _
"string1-2,string2-2,2" & @crlf & _
"string1-3,string2-3,3" & @crlf & _
"string1-4,string2-4,4"

Msgbox(0,"", $txt)

$a = StringRegExpReplace($txt, '(?m)^(.*),(.*),(.*)$', ',,$1,,$2,$3')   ; arrange
$tmp = @TempDir & "\temp.txt"
FileWrite($tmp, $a)
Local $res
_FileReadToArray($tmp, $res, 0, ",")
FileDelete($tmp)
_ArrayDisplay($res)
Edited by mikell

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  

×