iloveyou Posted March 9, 2016 Share Posted March 9, 2016 My data is stored in arrays but it doesn't appear that I can write to a file. Is there anyway to do this? Perhaps convert array into a string write to file then convert string into array? Thank you Link to comment Share on other sites More sharing options...
InunoTaishou Posted March 9, 2016 Share Posted March 9, 2016 (edited) #include <File.au3> #include <Array.au3> ; List all the files in the current script directory. Local $aScriptDir = _FileListToArray(@ScriptDir) ; Create a file in the users %TEMP% directory. Local $sFilePath = @TempDir & "\Examples.txt" ; Write array to a file by passing the file name. _FileWriteFromArray($sFilePath, $aScriptDir, 1) ; Display the file. ShellExecute($sFilePath) Local $aNewScriptDir _FileReadToArray($sFilePath, $aNewScriptDir) _ArrayDisplay($aNewScriptDir) Edited March 9, 2016 by InunoTaishou iloveyou 1 Link to comment Share on other sites More sharing options...
iloveyou Posted March 9, 2016 Author Share Posted March 9, 2016 I can't use that method because I need to keep my key value pairs. I have resorted to using _ArrayToString but how can I turn the string back into an array? The issue is that I have 2d array so just using StringSplit doesn't work.. Here is what my ini file looks like [General] $totalFrankingCredits= $currentMoney=1000 $pendingDivPayments=Code|Pay Date|Payment amount DCK|2016/01/01|500 BCG|2016/03/01|100 $divPaymentHistory= $holdings=Code|Number of Stocks CBA|1000 RIO|50 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 9, 2016 Moderators Share Posted March 9, 2016 iloveyou, Quote My data is stored in arrays but it doesn't appear that I can write to a file Here is what my ini file Where is this data that you want to store? In an array or in this ini file? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Myicq Posted March 9, 2016 Share Posted March 9, 2016 a long time ago, when I was doing some coding in Access VBA, I needed to access some settings in a simple way. Essentially a bit like your setup where you have an array and key, and need to get info. In your place, I would look at SQLite functions, and develop a few wrapper functions to read and store info. Then forget about how data is stored, you would simple be able to do ; read value $myAmount=_get("pendingDivPayments","DCK") ; set value _set("pendingDivPayments","DCK", "500") There are also projects around here to deal with associative arrays, personally I like the SQLite approach better. Just some random ideas. I am just a hobby programmer, and nothing great to publish right now. Link to comment Share on other sites More sharing options...
AutoBert Posted March 9, 2016 Share Posted March 9, 2016 You can use IniReadSection to read a whole section into Ini. You can use StringSplit for getting arrays for cases Keyvalue is a stored array: #include <Array.au3> $aGeneral=IniReadSection('ily.ini','General') _ArrayDisplay($aGeneral,'[General]') $aPendingDivPayments=StringSplit($aGeneral[3][1],'|') _ArrayDisplay($aPendingDivPayments,'PendingDivPa Btw.: your INI isn't valid, these lines: DCK|2016/01/01|500 BCG|2016/03/01|100 CBA|1000 RIO|50 couldn't read with IniReadSection or IniRead. Link to comment Share on other sites More sharing options...
czardas Posted March 9, 2016 Share Posted March 9, 2016 (edited) Perhaps you should try _ArrayToDeclarationString() and _ArrayDeclareFromString() by jguinch. I quite like that.https://www.autoitscript.com/forum/topic/179779-_arraydeclarefromstring-_arraytodeclarationstring-_arrayshufflemultidim-_arraycompare-_arrayenumvalues-_arrayassign/ Edited March 9, 2016 by czardas jguinch 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
iloveyou Posted March 9, 2016 Author Share Posted March 9, 2016 Basically I have $pendingDivPayments => [['Code','Pay Date','Payment amount'],['DCK',"2016/01/01",500],['BCG','2016/03/01',100]] Then I convert array to a string inorder to write to the .ini file. _ArrayToString($pendingDivPayments) ;returns this Code|Pay Date|Payment amount DCK|2016/01/01|500 BCG|2016/03/01|100 I am not sure why there are spaces?? Link to comment Share on other sites More sharing options...
czardas Posted March 9, 2016 Share Posted March 9, 2016 (edited) The line breaks could be something to do with the forum. This gives no extra line breaks. #include <Array.au3> Local $pendingDivPayments = [['Code','Pay Date','Payment amount'],['DCK',"2016/01/01",500],['BCG','2016/03/01',100]] _ArrayDisplay($pendingDivPayments) MsgBox(0, "", _ArrayToString($pendingDivPayments)) I don't understand why some people consider the ini format to be a suitable way to store the contents of an array in a file: your numbers will be converted to strings. Edited March 9, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jguinch Posted March 9, 2016 Share Posted March 9, 2016 You can try _ArrayToDeclarationString and _ArrayDeclareFromString functions from my ArrayMultiDim UDF (look at the examples). I made those functions for this use case. Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
iamtheky Posted March 9, 2016 Share Posted March 9, 2016 (edited) Also, using the dictionary to feign maplike functions will allow you to send it to a 2Darray and back with minimal effort Edited March 9, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iloveyou Posted March 9, 2016 Author Share Posted March 9, 2016 45 minutes ago, jguinch said: You can try _ArrayToDeclarationString and _ArrayDeclareFromString functions from my ArrayMultiDim UDF (look at the examples). I made those functions for this use case. If I call _ArrayToDeclarationString twice it seems to add the contents of the its previous call. I end up with something like this [["Code","Pay Date","Payment amount"],["DCK","2016/01/01",500],["BCG","2016/03/01",100]] ; first call ; second call, this should only have "[["Code","Date","Payment amount"]]" [["Code","Pay Date","Payment amount"],["DCK","2016/01/01",500],["BCG","2016/03/01",100]][["Code","Date","Payment amount"]] Is there a way to work around this? Link to comment Share on other sites More sharing options...
jguinch Posted March 9, 2016 Share Posted March 9, 2016 (edited) Sorry iloveyou, it was a bug. I edited my UDF. Can you retry ? @czardas : I didn't see your post in #7 : cool ! Edited March 9, 2016 by jguinch czardas 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
iloveyou Posted March 10, 2016 Author Share Posted March 10, 2016 hm it is a bit hard since I already developed a work around thanks for fixing it anyway. Link to comment Share on other sites More sharing options...
mikell Posted March 10, 2016 Share Posted March 10, 2016 (edited) Not sure I understood well, but if the purpose is to write in a .ini, isn't this easier ? #Include <Array.au3> Local $pendingDivPayments = [['Code','Pay Date','Payment amount'],['DCK',"2016/01/01",500],['BCG','2016/03/01',100]] ;_ArrayDisplay($pendingDivPayments) ;Code|Pay Date|Payment amount ;DCK|2016/01/01|500 ;BCG|2016/03/01|100 $ini = "test.ini" For $i = 1 to UBound($pendingDivPayments)-1 For $j = 0 to UBound($pendingDivPayments, 2)-1 IniWrite($ini, "my_section" & $i, $pendingDivPayments[0][$j], $pendingDivPayments[$i][$j]) Next Next #cs result in the ini : [my_section1] Code=DCK Pay Date=2016/01/01 Payment amount=500 [my_section2] Code=BCG Pay Date=2016/03/01 Payment amount=100 #ce Edit or is it this ? Local $pendingDivPayments = [['Code','Pay Date','Payment amount'],['DCK',"2016/01/01",500],['BCG','2016/03/01',100]] $txt = "" For $i = 0 to UBound($pendingDivPayments)-1 For $j = 0 to UBound($pendingDivPayments, 2)-1 $txt &= $pendingDivPayments[$i][$j] & (($j=UBound($pendingDivPayments, 2)-1) ? @crlf : "|") Next Next Msgbox(0,"", $txt) ;Code|Pay Date|Payment amount ;DCK|2016/01/01|500 ;BCG|2016/03/01|100 Edited March 10, 2016 by mikell Link to comment Share on other sites More sharing options...
AutoBert Posted March 10, 2016 Share Posted March 10, 2016 when in Inifile than like this: #include <Array.au3> Dim $aPendingDivPayments[][] = [['Code', 'Pay Date', 'Payment amount'], ['DCK', "2016/01/01", 500], ['BCG', '2016/03/01', 100]] Global $aCSV IniWrite('ily.ini', 'General', 'pendingDivPayments', _ArrayToString($aPendingDivPayments, '|', -1, -1, '<CrLf>')) _IniReadTo2dArray('ily.ini', 'General', 'pendingDivPayments', $aCSV) _ArrayDisplay($aCSV) Func _IniReadTo2dArray($sIniPath, $sSection, $sKey, ByRef $aCSV) local $sText = IniRead($sIniPath, $sSection, $sKey, '') ConsoleWrite($sText & @CRLF) local $aCSV = StringSplit($sText, '<CrLf>', 1) _ArrayColInsert($aCSV, 1) local $iActCols = 2, $aSplit For $i = 1 To $aCSV[0][0] $aSplit = StringSplit($aCSV[$i][0], '|') If IsArray($aSplit) Then If $aSplit[0] > $iActCols Then _ArrayDisplay($aSplit) For $j = $iActCols To $aSplit[0] - 1 _ArrayColInsert($aCSV, $j) Next $iActCols = $aSplit[0] EndIf For $j = 0 To $aSplit[0] - 1 $aCSV[$i][$j] = $aSplit[$j + 1] Next EndIf Next EndFunc ;==>_IniReadTo2dArray but remember Original IniFile functions only read ~32 kb Link to comment Share on other sites More sharing options...
iloveyou Posted March 10, 2016 Author Share Posted March 10, 2016 Thank you guys for your responses.. really. But I have already solved this problem with jguinch's functions. They just had a slight bug but from what I hear he has fixed them now. <3 czardas 1 Link to comment Share on other sites More sharing options...
iamtheky Posted March 10, 2016 Share Posted March 10, 2016 (edited) Threads will live long after you have had your way with them, ask a regexp question and it will live forever. I find it best to copy and store all correct answers, in hopes i one day have that question. Edited March 10, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted March 10, 2016 Share Posted March 10, 2016 iamtheky, I totally agree IMHO it's a much better way for an efficient learning than to blindly use UDFs which are certainly very nice but whose you don't understand how they work Link to comment Share on other sites More sharing options...
BrewManNH Posted March 10, 2016 Share Posted March 10, 2016 I only use the Array udf as a last resort myself, so I can understand that thinking. 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...
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