Hoth2001 Posted September 6, 2017 Share Posted September 6, 2017 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 More sharing options...
FrancescoDiMuro Posted September 6, 2017 Share Posted September 6, 2017 (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 September 6, 2017 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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! Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 For Planet "Me" that is an abbreviaton for Mercury.....to help provide some clarification Link to comment Share on other sites More sharing options...
BrewManNH Posted September 6, 2017 Share Posted September 6, 2017 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 GudeHow 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 More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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? Link to comment Share on other sites More sharing options...
BrewManNH Posted September 6, 2017 Share Posted September 6, 2017 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 GudeHow 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 More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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 More sharing options...
iamtheky Posted September 6, 2017 Share Posted September 6, 2017 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 More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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! Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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 Link to comment Share on other sites More sharing options...
iamtheky Posted September 6, 2017 Share Posted September 6, 2017 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 More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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 More sharing options...
iamtheky Posted September 6, 2017 Share Posted September 6, 2017 (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 September 6, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 $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 More sharing options...
iamtheky Posted September 6, 2017 Share Posted September 6, 2017 (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 September 6, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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: Link to comment Share on other sites More sharing options...
iamtheky Posted September 6, 2017 Share Posted September 6, 2017 (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 September 6, 2017 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Hoth2001 Posted September 6, 2017 Author Share Posted September 6, 2017 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????? Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted September 13, 2017 Share Posted September 13, 2017 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 ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now