Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 07-14-2018, 10:02 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default Q: how to replace a number at the end of a line in a Track Chunk

Can someone point me in the right direction?

I would like to find the occurrence of a unique alphanumeric string in a TrackStateChunk and replace it by a longer alphanumeric string.

I have tried a few things but got myself confused. Is there a good lua example to look at (and "adopt and adapt")?

--------------
Also, I have an absolute blind spot: How do I put a number into a string, in lua?
e.g.
numQs = 27
desired_result = "I have 27 interesting questions"
__________________
DarkStar ... interesting, if true. . . . Inspired by ...

Last edited by DarkStar; 07-16-2018 at 07:07 AM.
DarkStar is online now   Reply With Quote
Old 07-14-2018, 10:28 AM   #2
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

1)
are you using string.gsub?

http://lua-users.org/wiki/StringLibraryTutorial


2)
desired_result = "I have ".. numQs .." interesting questions"
you mean this?
deeb is offline   Reply With Quote
Old 07-14-2018, 03:49 PM   #3
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default

Thank you.

1) I wasn't but am trying it out now - looks promising
2) You mean I just concatenate (..) them together?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Old 07-14-2018, 04:42 PM   #4
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

Yep, read here: https://www.lua.org/pil/3.4.html
deeb is offline   Reply With Quote
Old 07-14-2018, 05:24 PM   #5
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

If there's any chance of the value not being a number, or once in a while I could swear Lua gets mad even when I know it IS a number, it's never a bad idea to do:

desired_result = "I have ".. tostring(numQs) .." interesting questions"

- Booleans and nils will be converted to "true", "false", "nil".

- Note that this isn't the same as wrapping the entire statement in tostring(). In that case, if numQs was accidentally nil you'd crash the script by trying to concatenate a nil value, exactly as if the tostring wasn't there at all.
__________________
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 07-14-2018, 07:25 PM   #6
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

or

Code:
numQs = 27 -- try: nil or non number like "a"
if type(numQs) == "number" then 
    desired_result = "I have " .. numQs .." interesting questions"
    print (desired_result)
else  
    print ("undefined results")
end
here: http://rextester.com/FTRB68692
deeb is offline   Reply With Quote
Old 07-14-2018, 10:33 PM   #7
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

string = string:gsub('VALUE %d', 'VALUE 5')

What are you tring to do exactly?
mpl is offline   Reply With Quote
Old 07-15-2018, 06:27 AM   #8
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default

Guys, thank you for the insights.

Yep, I want to change one set of characters at the end of a line to another; the syntax I ended up with is:
Code:
old2 = "123"
new2 = 456"
chunk13, num = string.gsub(chunk,old2,new2)
And that works, apart from one thing - if old2 is "123" and the chunk contains 1234<eol> and 123<eol> then both will be changed. What I need to do is add the <eol> character to the string, so that I only change 123<eol> But I cannot find out how that <eol> is represented in the chunk.

Have a look at this: it is the first few lines of a track in the .rpp, as seen in Notepad, the ReaScript Editor and a hexadecimal Editor.

--------------------
PS What is the difference between "string.gsub" and "string:gsub"?

>>> https://i.imgur.com/0Jrkay1.png

In the first and the last, the characters are CR, LF and 4 spaces. But in the ReaScript Editor, they occupy 1 non-printable position (see the green arrows). I have found that it is a single character by doing FINDs on the preceding and succeeding visible characters.

Does anyone know how it is represented. And how I would add that to my Old2 and new2 strings?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Old 07-15-2018, 06:50 AM   #9
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Try:
Code:
old2 = "123[\n\r]"
new2 = "456[\n\r]"
The square brackets tell it you're defining a set of characters, \n is a new line and \r is a carriage return. Likewise, splitting a string by line can be done by negating that set with ^, like:
Code:
for line in string.gmatch(str, "[^\n\r]+")
__________________
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 07-15-2018, 07:04 AM   #10
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

It was brought to my attention here:
https://forum.cockos.com/showthread.php?t=175403
That different OS may have different eol.

That's all I got.

There's no way to do what you want without "chunk editing hell" as mpl has put it?
FnA is offline   Reply With Quote
Old 07-15-2018, 07:16 AM   #11
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

You should reference to key-value pair instead just number at the end of line. Are you sure you haven`t existed API for what you need?

"string.gsub" is calling a function and "string:gsub" is calling a method.

some_string:gsub('something', 'something_else') is the same as string.gsub(some_string, 'something', 'something_else')
mpl is offline   Reply With Quote
Old 07-15-2018, 09:20 AM   #12
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default

Quote:
Originally Posted by mpl View Post
You should reference to key-value pair instead just number at the end of line.
Sorry, I do not know what you mean.
Quote:
Originally Posted by mpl View Post
Are you sure you haven`t existed API for what you need?
Pretty sure.

Quote:
Originally Posted by mpl View Post
"string.gsub" is calling a function and "string:gsub" is calling a method.

some_string:gsub('something', 'something_else') is the same as string.gsub(some_string, 'something', 'something_else')
What is the difference between a function and a method. Which is preferred?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Old 07-15-2018, 09:25 AM   #13
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default

OK, here's a more understandable example - replacing MIDI learn data (in the PARMLEARN lines). Here's a screenshot of my Script testing and the results:

>>> https://i.imgur.com/tv8RqZ7.png

nn00 is the pattern to be found - there are two occurrences of it.

nn10 to nn21 are FINDs with various suffix after the main string. With A LOT of trial and error I have found that the <eol> sequence is a space [#] followed by a line feed. nn18 shows that. Leading spaces on the next line are dropped, as shown by the suffixes for nn20 and nn21.

The breakthrough was when looking at the substrings in nn30 to nn41. They start with the found string and advance it by one position each time.
  • nn35 starts with the 1 at the end of that PARMLEARN line
  • nn36 starts with a non-printing character, but it takes up a character position
  • nn37 starts with the "P" from the next line
  • nn38 starts with the "P" from the next line but has 1 more character at the end
This started me thinking that perhaps that first character in nn36 was, in fact, a space [#]. When that worked, I followed it by the various control characters.

Phew!

Now to see if it works in my real ReaScript.

---------------
Edit:
[#] On further testing, the space may not be there, but the line feed is.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...

Last edited by DarkStar; 07-16-2018 at 02:37 AM.
DarkStar is online now   Reply With Quote
Old 07-15-2018, 10:46 AM   #14
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

maybe you should build your test case in: http://rextester.com/
save it and share permanent link you get when saving (see above save button), as it will be easier for anyone to help.

you can make a string with multiline content for the chunk like this:

Code:
 
chunk = [===[
This is a long multiline string which you can use for your chunk example
which will end up in a variable ]===]

print (chunk)
see here:
http://rextester.com/VJUV66004

also, just in case, to get your chunk into a file you can use:
Code:
local file = io.open("myChunkFile.txt", "w")
file:write(chunk)
file:close()
deeb is offline   Reply With Quote
Old 07-15-2018, 11:13 AM   #15
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

https://github.com/ReaTeam/ReaScript...DIOSCLearn.lua (pretty rough old example and I don`t recommend use it but mostly works)

Also there are extended examples in ReaPack/mpl_LearnManager.lua, but it require some understanding mechanism of how this script works, how it build mapping from raw table data etc

Quote:
Originally Posted by DarkStar View Post
OK, here's a more understandable example - replacing MIDI learn data (in the PARMLEARN lines). Here's a screenshot of my Script testing and the results:...
You are playing very dangerous game here.

In short, safety way I can suggest here is:
1) get track state chunk
2) grab FX chunk (string beetween 'FXGUID actual_FX_GUID' and 'WAK'). WAK is standing for 'Send all keyboard input' and always exists in FX chunk.
3) find PARMLEARN strings
4) parse them completely
5) replace what you want to replace
6) collect PARMLEARN strings back
7) replace them in FX chunk relatively using literalize function (otherwise you can`t replace since some symbols can be lua magic characters for gsub patterns)
8) replace modified FX chunk in track state chunk (also with literalize)

I can help with it if needed.

Last edited by mpl; 07-15-2018 at 01:24 PM.
mpl is offline   Reply With Quote
Old 07-16-2018, 07:15 AM   #16
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,679
Default

That could be the way to go, but it looks like a lot of additional code. What would be the dangers of a string.gsub (as long as I make sure I have got the strings exactly right)?

Teaser:
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Old 07-16-2018, 10:21 AM   #17
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

Quote:
Originally Posted by DarkStar View Post
What would be the dangers of a string.gsub (as long as I make sure I have got the strings exactly right)?
Exact string somewhere you don't expected it can be. In any way you need to use chunking + gsub, so it is better to use it safely.
mpl is offline   Reply With Quote
Old 07-16-2018, 02:06 PM   #18
lb0
Human being with feelings
 
Join Date: Apr 2014
Posts: 4,175
Default

Quote:
Originally Posted by FnA View Post
It was brought to my attention here:
https://forum.cockos.com/showthread.php?t=175403
That different OS may have different eol.

That's all I got.

There's no way to do what you want without "chunk editing hell" as mpl has put it?
Well you can search for pattern \r?\n - this works for me across multiple OS's (apparently - I don't have a Mac and haven't tried Linux for some years - but a Mac user said it produced the correct result when my code was not working beforehand)
__________________
Projects - Reascripts - Lua:
Smart Knobs 2 | LBX Stripper | LBX Floating FX Positioner
Donate via Paypal | LBX Tools Website
lb0 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 07:56 AM.


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