Old 04-20-2017, 10:51 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default lua: Saving a sparse array into the project file?

Can I do this? If so, how?

Let's say I have an array of values stored in x[10] = 1, x[20] = 2, x[30] =3 and x[31] =4. The 'tens digit is the track number.

What is the best way to save these values into the project file and to recall them?

And how would I do it in EEL?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 04-20-2017, 11:09 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Storing data to projects uses: reaper.SetProjExtState( proj, extname, key, value )

The simplest way would be to just store a separate string for each key/value pair:

Code:
for k, v in pairs(x) do
  reaper.SetProjExtState( 0, "DarkStar's cool script", k, v )
end
To reload them (I'm just typing from memory, not sure if this example works):
Code:
local x = {}
local idx = 0 -- Might start from 1? Not sure.

while true do
  local ret, key, val = reaper.EnumProjExtState( 0, "DarkStar's cool script", idx )
  if not ret then break end
  
  -- ExtState values are always strings, so you have to convert them
  x[tonumber(key)] = tonumber(val)

  idx = idx + 1
end
Obviously, if you're storing other data as well you'll have to check the string content yourself to decide what to do with it. Maybe store the indices with keys like keyXX, use an if string.match("^key", key) then... when reloading, and then strip off the key before converting it to a number?

A less cluttery approach would be to serialize the table so you only have to store one ProjExtState instead of a whole bunch. I prefer the serializeTable function given here: http://stackoverflow.com/questions/6...able-functions , but there are plenty of ways to do it. I think this one: http://lua-users.org/wiki/PickleTable is pretty common among ReaScripters.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 04-21-2017, 03:41 AM   #3
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

Thank you for the insight and examples - I'll need to do some reading.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 04-22-2017, 11:15 AM   #4
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

I'm getting there - I have now written the data into the project file, but am having trouble getting it back, using a match statement

Part of the data has the format:
Code:
"2.0 VST: ReaEQ (Cockos): 34.0" -- (stored in s1)
and I want to match the 2 numbers into variables and the FX type / name into a third. I have got the two numbers sorted out but cannot work out how to match the other characters - a mix of upper and lower case, one or more words, with : and ( and ).

Can someone let me know a good match statement? So far I have:
Code:
ZA, ZB, ZC = s1:match("(%d%p%d) something (%d%d%p%d)
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 04-22-2017, 03:08 PM   #5
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Try this:
Code:
local ZA, ZB, ZC = s1:match("([%d%.]+) (.+) ([%d%.]+)")
ZA -> [Either a digit or a .] x one or more
ZB -> [Literally anything] x one or more
ZC -> [Either a digit or a .] x one or more
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 04-24-2017, 04:27 AM   #6
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

Phew! Thank you. I did try %. before but could not get it right. That leads to further questions
(a) I had thought that (.+) would match all the rest of the string?
(b) and %. matches a . and . matches anything at all?

Is there a GOOD guide to match patterns? - I've looked at both the lua Reference and Programming in lua but they did not help much.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...

Last edited by DarkStar; 04-24-2017 at 10:34 AM.
DarkStar is offline   Reply With Quote
Old 04-24-2017, 10:21 AM   #7
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

http://wiki.roblox.com/index.php?title=String_pattern

a) As I said, my understanding is a bit shaky, but basically match will try to satisfy all of the conditions you specify in the pattern. In this case, that translates as:

"A number with a decimal" space "As many characters as possible" space "A number with a decimal"

Given the format of your string, the only possible way to match it is by leaving the last set of characters for the third variable. If .+ grabbed everything, it wouldn't be able to match the third capture.

I'm not sure if .- would behave differently in this situation, or if it would even work, but it might be a more error-proof way to go.

There are also special characters for the beginning (^) and end ($) of a string that can come in handy, particularly with file paths.

b) Correct. For any of the characters that have a special meaning (^$()%.[]*+-?), you can add a % in front to specify that you mean the character itself - much like how you have to use \\ as folder separators in Windows paths so Lua doesn't get confused.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 05:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.