Sign in to follow this  
Followers 0
Hoth2001

2d array problems from scv file

20 posts in this topic

I'm trying to take a csv file that looks like this:

Planet,Max,Month,Day,Year
Me,7,01,01,2011
Me,10,01,01,2011

and turn it into a 2d array thatI will be able to call on the values in the array throughout the rest of my program.

as you can see the array should have 5 columns 

I'm a newbie so the CSV.au3 file by ProgAndy went over my head. Furthermore, much of the previous posts I've researched seem to steer clear from the basics.  

 

I've spent hours researching and reading posts! I'm at the point where sample code would be MOST APPRECIATED.....I've put together the following code that :

Dim $avPlanets[$text[0] + 1][2] = [["Planet", "Max"]]

;I'm not sure of the syntax on how to control the number of columns for the array!
For $x = 1 To $text[0]
    $array = StringSplit($text[$x], ",")
    If $array[0] = 2 Then
        $avPlanets[$x][0] = $array[1]
        $avPlanets[$x][1] = $array[2]
    EndIf
Next
_ArrayDisplay($avPlanets, "Debug: $avPlanets")

Please help!

much of the documentation I keep running into seems to focus more on 1d arrays than 2d arrays.  

Any help would be GREATLY appreciated

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Good morning @Hoth2001, and welcome to the AutoIt forum :)

I read your post, but, I have to ask a couple questions:

1) Where does $text come from? 

2) Which values $text contains?

3) Same questions above for $array.

Provide us as much information as you can :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

THank you for the warm welcome!

Ok...well The text is coming from a csv flle.  I have uploaded screenshots of what the csv file looks like in either excel or notepad.

 

I want to have autoit read that csv file into a 2d array.  The code I have above is probably all wrong.  Please help!

 

 

CSVscreenshot.png

CSVscreenshot2.png

Share this post


Link to post
Share on other sites

For Planet "Me"  that is an abbreviaton for Mercury.....to help provide some clarification

Share this post


Link to post
Share on other sites

Look at _FileReadToArray and look at the 4th parameter for splitting a CSV into a 2D array.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Right....but when I read _FileReadToArray this is what I see:

No parameters to think of...which has me a little confused.  Are you getting your information from another location I'm not aware of?

 

FileRead.png

Share this post


Link to post
Share on other sites

No, that's FileReadToArray, not _FileReadToArray, there are 2 different functions.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Wow! that's a big help BrewManNH!!!!!

Ok...so looking at the code example given:  

; Create "square" 2D array
    Local $aArray[][] = [ _
            ["00", "01", "02", "03"], _
            ["10", "11", "12", "13"], _
            ["20", "21", "22", "23"], _
            ["30", "31", "32", "33"]]
    _ArrayDisplay($aArray, "Original", Default, 8)
    ; Write it to file
    _FileWriteFromArray($sFilePath, $aArray, Default, Default, ",")
    Sleep(1000)

    ; Re-read it - with count
    _FileReadToArray($sFilePath, $aRetArray, Default, ",")
    _ArrayDisplay($aRetArray, "2D array - count", Default, 8)

    ; Re-read it - without count
    _FileReadToArray($sFilePath, $aRetArray, $FRTA_NOCOUNT, ",")
    _ArrayDisplay($aRetArray, "2D array - no count", Default, 8)

    ; Read into "array of arrays" with count
    _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT + $FRTA_INTARRAYS, ",")
    _ArrayDisplay($aRetArray, "Array of arrays - count", Default, 8)
    ; Now look inside the arrays inside the returned array
    _ArrayDisplay($aRetArray[1], "Array 1 inside RetArray - count", Default, 8)

 

Let me know if my understanding is correct:

I would have to first create a 2d array variable in Autoit

then essentially populate it with my csv file???

 

Not exactly sure what  is meant by "with count" or "without count" in the code.   

If anyone could shed light on my logic MUCH appreciated

Share this post


Link to post
Share on other sites

you just have to declare an array, the example in the file just builds one and writes it to a file to give you something to work with.  you just need:

#include<file.au3>

local $aArray

_FileReadToArray("c:\somedir\somesubdir\somefile.txt" , $aArray)

_ArrayDisplay($aArray)

 

and to repeat the helpfile, but count just makes the array 1-based and the number of elements in that array is what populates $aArray[0], with NoCount the Array elements start at 0 and there is no count.  It's easier to discern the first time when it's not an array of numbers.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#10 ·  Posted

Ok. THANK YOU!  

I added in the comma delimiter that I needed  to the code

#include<file.au3>

local $aArray

_FileReadToArray("C:\Users\User\Desktop\Planets.csv" , $aArray, 0, ",")

_ArrayDisplay($aArray)

and got exactly what I was looking for!!!

Now....i'm going to tackle how to manipulate this data for this program I'm trying to write!

Array.png

Share this post


Link to post
Share on other sites

#11 ·  Posted

Ok ......how to I use the different elements of the array as needed in a program??

For example, I want to assign an element to a variable and then check to see if I have isolated the right element by writing the result to the console

But .....alas.....my feeble coding skills hit another wall:

#include<file.au3>

local $aArray

_FileReadToArray("C:\Users\User\Desktop\Planets.csv" , $aArray, 0, ",")

;~ _ArrayDisplay($aArray)

$Var = $aArray[1][1]

ConsoleWrite("$Var")

it seems that in the console "7" should appear based on the array I created

help

Array.png

Share this post


Link to post
Share on other sites

#12 ·  Posted

close:

#include<array.au3>

local $aArray[0][5]

_ArrayAdd($aArray , "Planet | Max | Month | Day | Year")
_ArrayAdd($aArray , "Mercury | 7 | 1 | 1 | 2011")
_ArrayDisplay($aArray)


$Var = $aArray[1][1]

ConsoleWrite($Var)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#13 ·  Posted

I'm confused....in your sample code I would have to manually construct the array.

What I thought I just did was read data from an external file  into an array from this line of code

_FileReadToArray("c:\somedir\somesubdir\somefile.txt" , $aArray)

 

Afterwards I'm trying to then use an element from the array that was created from the external file

Am I missing something??

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

that my var in consolewrite wasnt in quotes.

When you build an array from a file, and dont provide the file, we have to build it manually.  When the array exists in the code everyone can help.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#15 ·  Posted

$Var = $aArray[1][1]

ConsoleWrite($Var)

even without the quotes I don't get any output in the console.

In the future, my csv file will have hundreds of lines....so I hope I won't have to construct anything manually!!

I've attached the sample csv file (I think that's what you were suggesting)

 

How would I disply the element I'm trying to isolate into console?

 

Planets.csv

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

what are you seeing the console?  nothing?

when you run my script in post #12 do you see'7' in the console?

and i have found you are more likely to get assistance if you just have a small section that highlights the issue.  

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#17 ·  Posted

I get nada!   

Here is the code

#include<file.au3>

local $aArray

_FileReadToArray("C:\Users\User\Desktop\Planets.csv" , $aArray, 0, ",")

;~ _ArrayDisplay($aArray)

$Var = $aArray[1][1]

ConsoleWrite($Var)

 

and here is the console output:

 

Console.png

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Look at the first character of the 3rd line from the bottom :)

You can add a carraige return line feed for clarity

consolewrite($Var & @CRLF)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#19 ·  Posted

jumpin' Jahoozifats batman!!

There it is!!

I'm guessing now is the time for me to learn how to add a linebreak macro into the script

Its something like @CRLF?????

 

 

Share this post


Link to post
Share on other sites
On 6/9/2017 at 10:46 PM, Hoth2001 said:

I'm guessing now is the time for me to learn how to add a linebreak macro into the script

Ehm?!

On 6/9/2017 at 10:46 PM, Hoth2001 said:

Its something like @CRLF?????

@CRLF, @CR, @LF?


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

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