Jump to content

2d array problems from scv file


Hoth2001
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

ALWAYS GOOD TO READ:

 

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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.

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

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

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