Jump to content

A way to store arrays


Recommended Posts

#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 by InunoTaishou
Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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

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 by czardas
Link to comment
Share on other sites

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

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 by czardas
Link to comment
Share on other sites

Also, using the dictionary to feign maplike functions will allow you to send it to a 2Darray and back with minimal effort

 

 

 

Edited by iamtheky

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

Link to comment
Share on other sites

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

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 by mikell
Link to comment
Share on other sites

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

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 by iamtheky

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

Link to comment
Share on other sites

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

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