I've been breaking my teeth on how to best save an array with key/value pairs and retrieve/update them. I am using rosv7.11 and tried many combinations but somehow i can't seem to retrieve the data and access individual key/value pairs.
The datafile is saved as dataUsage and contains the following {month=8;total=631}
Code: Select all
:local currentDate [/system clock get date];
:local currentMonth [:tonum [:pick $currentDate 5 7]];
:local dataFile "dataUsage.txt";
:if ([/file find name=$dataFile] = "") do={
# datausage.txt file does not exist so create with base values
/file add name=$dataFile contents="{month=$currentMonth;total=0}";
} else {
:local fileContent [/file get $dataFile contents];
:log info [:typeof $fileContent]; # returns string
:local fileData [:toarray $fileContent];
:log info [:typeof $fileData]; # returns array
:local fileMonth [:tonum ($fileData->"month")];
:log info ($fileData->"month"); # returns blank
:log info ($fileData->"total"); # returns blank
:if ($fileMonth=$currentMonth) do={
:log info "Same month";
# ... processing to be added
} else {
:log info "Different month $fileMonth/$currentMonth "; # $fileMonth is blank
:set ($fileData->"month") $currentMonth;
:set ($fileData->"total") 0;
}
/file remove $dataFile;
/file add name=$dataFile contents="{month=$($fileData->"month");total=$($fileData->"total")}";
}
Any insight would be much appreciated or if there is a better way to save and restore a few key/value pairs so that they persist at reboot that would be great.
Thanks
Michaël