Jump to content


Photo

Convert a string to a table. Extract XML data...

string table

  • Please log in to reply
14 replies to this topic

#1 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 09 August 2018 - 17:09

I need to convert a string into a table.

 

The string: string = "8,34,255"

 

The resulting table: table = {8,34,255}

 

What is the best method to do that? Does not LUA have anything direct for such a conversion?

 

Each number separated by a comma can be 1, 2 or 3 characters. For example:

  1. "1,10,100"
  2. "10,100,1"
  3. "100,10,1"
  4. etc

Could this be useful?:

-- Create a new, or convert an exiting table to an object that uses the global
-- 'table.XXX' functions as methods, just like strings in Lua do.
-- > examples: t = table.create(); t:insert("a"); rprint(t) -> [1] = a;  
--           t = table.create{1,2,3}; print(t:concat("|")); -> "1|2|3";
table.create([t]) -> [table]

Thanks!


Edited by Raul (ulneiz), 13 August 2018 - 11:55.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#2 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1863 posts
  • Gender:Not Telling

Posted 09 August 2018 - 17:51

Nope. Use gmatch like it says here: https://stackoverflo...ated-with-comma

 

local output = { }
local str = "10,100, 1"

for match in str:gmatch("([^,%s]+)") do
  output[#output+1] = tonumber(match)
end


#3 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 10 August 2018 - 02:42

 

Nope. Use gmatch like it says here: https://stackoverflo...ated-with-comma

local output = { }
local str = "10,100, 1"

for match in str:gmatch("([^,%s]+)") do
  output[#output+1] = tonumber(match)
end

 

Thanks! It seems one of the most direct ways. I've seen several weird or overly extensive things looking for google.

 

I have used something very similar. The result of the function:

--capture the native color of mark (C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1\Config.xml)
function crk_capture_clr_mrk()
  --print ( os.currentdir() )
  local filename = ( "%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
  print( filename )
  
  --RenoisePrefs
    --SkinColors
      --Selected_Button_Back

  local pref_data = renoise.Document.create("RenoisePrefs") { SkinColors = { Selected_Button_Back = "" } }
  pref_data:load_from( filename )
  --print( pref_data.SkinColors.Selected_Button_Back )
  local rgb = tostring(pref_data.SkinColors.Selected_Button_Back)
  local one, two, thr = rgb:match( "([^,]+),([^,]+),([^,]+)" )
  CRK_CLR.WHITE[1] = tonumber(one)
  CRK_CLR.WHITE[2] = tonumber(two)
  CRK_CLR.WHITE[3] = tonumber(thr)
end
crk_capture_clr_mrk()

The table CRK_CLR.WHITE get the RGB values of the "Selected_Button_Back" inside the Config.xml.

 

Config.xml:

<!--Skin Colors-->
  <SkinColors>
    <Main_Back>16,16,16</Main_Back>
    <Main_Font>219,219,219</Main_Font>
    <Alternate_Main_Back>15,16,16</Alternate_Main_Back>
    <Alternate_Main_Font>212,212,212</Alternate_Main_Font>
    <Body_Back>64,64,64</Body_Back>
    <Body_Font>170,170,170</Body_Font>
    <Strong_Body_Font>58,190,255</Strong_Body_Font>
    <Button_Back>76,77,77</Button_Back>
    <Button_Font>255,255,255</Button_Font>
    <Button_Highlight_Font>255,181,35</Button_Highlight_Font>
    <Selected_Button_Back>255,181,35</Selected_Button_Back>
    <Selected_Button_Font>24,24,24</Selected_Button_Font>

...

 

I have a little doubt with the path to Config.xml. os.getenv("APPDATA") also serves for OSX and Linux? Or is it necessary to restrict it only for Windows?

 

What are the path of Config.xml for OSX or for Linux?


Edited by Raul (ulneiz), 10 August 2018 - 02:47.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#4 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1863 posts
  • Gender:Not Telling

Posted 10 August 2018 - 11:19

Nobody knows. There's already a dedicated topic about the subject. http://forum.renoise...emeskin-colors/



#5 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 555 posts
  • Gender:Male

Posted 10 August 2018 - 12:44

As far as I know Raul with Renoise under Linux the Config.xml file is situated at (well on my machine anyway):

$HOME/.renoise/V3.1.1/Config.xml

Where $HOME is the users home directory.  So maybe(?) you could join a path string together like:

print(io.open(os.getenv("HOME").."/.renoise/V3.1.1/Config.xml"))

Just a thought.



#6 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 10 August 2018 - 13:34

I use Windows only. The idea is to use a function like this:

...

local rns_version = renoise.RENOISE_VERSION
local filename = ""

if ( os.platform() = "WINDOWS" ) then
  filename = ( "%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
end

if ( os.platform() = "MACINTOSH" ) then
 filename =   --the same for WINDOWS?
end

if ( os.platform() = "LINUX" ) then
 filename = ( "%s\\.renoise\\V%s\\Config.xml"):format( os.getenv("HOME"), rns_version )
end

...

The case is that it must works for the 3 operating systems, or at least that does not cause any error...

 

inside linux the route is like this exactly?: /.renoise/

 

I think Renoise has a corrector for the slash. If the path use \...\...\..., change automatically for /../../...  Unfortunately I can not try these things on other platforms than Windows.


Edited by Raul (ulneiz), 10 August 2018 - 13:38.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#7 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 555 posts
  • Gender:Male

Posted 10 August 2018 - 13:56

Remember Raul that on UN*X systems you tend to use forward slash to separate directories, so I'd probably write:

filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )

As for Mac, IDK.



#8 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 10 August 2018 - 14:12

Remember Raul that on UN*X systems you tend to use forward slash to separate directories, so I'd probably write:

filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )

As for Mac, IDK.

 

Thanks! The result:

 

Spoiler

 

Can you copy the print of entire path of os.currentdir() inside Linux? This should detect the type of operating system to return the path.

 

It would be great if there API was an os.preferdir() to direct to the Config.xml directory...


Edited by Raul (ulneiz), 10 August 2018 - 14:12.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#9 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 555 posts
  • Gender:Male

Posted 11 August 2018 - 07:04

This should detect the type of operating system to return the path.

I'm not sure what you mean here Raul, os.currentdir() just returns the current directory string (usually set to the directory that Renoise was launched from?)  So in my case (as I run Renoise from the terminal):

/home/4Tey/Applications/Renoise/renoise/

os.platform() gives you the OS string (as you are already doing?)
I think it is safer to say Raul that the path (whatever it is) for Mac would be closer to the Linux version rather than the Windows version, as MacOSX is a UN*X based BSD variant OS.
 
Ah, a sign..
[attachment=8188:thb.png]


Edited by 4Tey, 11 August 2018 - 07:04.


#10 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 11 August 2018 - 09:17

I'm not sure what you mean here Raul, os.currentdir() just returns the current directory string (usually set to the directory that Renoise was launched from?)  So in my case (as I run Renoise from the terminal):

/home/4Tey/Applications/Renoise/renoise/

os.platform() gives you the OS string (as you are already doing?)
I think it is safer to say Raul that the path (whatever it is) for Mac would be closer to the Linux version rather than the Windows version, as MacOSX is a UN*X based BSD variant OS.
 
Ah, a sign..

 

Nice image!  :D

-- Returns the platform the script is running on: "WINDOWS", "MACINTOSH" or "LINUX".
os.platform() -> [string]

-- Returns the current working dir. Will always be the scripts directory when executing a script from a file.
os.currentdir() -> [string]

I believe that the os.currentdir() only returns the current address if you print it from the terminal. If you execute it from a script, it will always return the entire path of the folder of your tool, compatible with the 3 operating systems.

 
Then, a new "os.preferdir()" would be feasible, which would return the preferences folder that contains the Config.xml file. In fact, I think it would be possible to extract the direction by cutting on the right the route of the os.currentdir().
 
For example, with Windows and from a script, os.currentdir() return the path:
 
C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1\Scripts\Tools\com.tool_name.xrnx\
 
and the path of Config.xml is:
 
C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1\Config.xml
 
I think that this piece \Scripts\Tools\com.tool_name.xrnx\ always has the same number of characters regardless of the operating system.
 
Maybe it would be possible to cut right to get the path of Config.xml from os.currentdir() and it would work for the 3 operating systems. It's just an idea.
 
...
 
By the way, I have the beach a few meters from my house. I live on the coast of the Mediterranean, east of Spain.
 
Edit: I need the print path within Linux using os.currentdir() since a script, not since the terminal. I need to know what happens from the right of the path.

Edited by Raul (ulneiz), 11 August 2018 - 09:23.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#11 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 11 August 2018 - 12:24

According to my search by google, it seems that the correct routes are the following:

 

  local filename = ""

  if ( os.platform() == "WINDOWS" ) then
    filename = ("%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
  elseif ( os.platform() == "MACINTOSH" ) then
    filename = ("%s/Library/Preferences/Renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )
  elseif ( os.platform() == "LINUX" ) then
    filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )
  end

  print(filename)

 

Spoiler

 

I think that this code should work with the 3 operating systems, also in older versions of Renoise, at least until version V2.6.0.

 

It would be nice if a MAC user and another LINUX user confirmed the path (for each case). It must be the folder that contains the Config.xml file!

 

...

 

This last function should also correctly return the path (filename). This method cuts the route by the word "Scripts". In theory, this folder is used in all operating systems, always after the Vx.y.z/.

function crk_test()
  local currentdir = os.currentdir()
  print( currentdir )
  local preferdir_length = string.find(currentdir, "Scripts" )
  print( preferdir_length )
  local preferdir = string.sub( currentdir, 1, preferdir_length -1 )
  print( preferdir )
  local filename = ("%sConfig.xml"):format( preferdir )
  print( filename )
end

Edited by Raul (ulneiz), 11 August 2018 - 12:26.

:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#12 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 555 posts
  • Gender:Male

Posted 11 August 2018 - 12:28

Ok, fair enough Raul, I can run os.currentdir from a tool.  I get:

/home/4Tey/.renoise/V3.1.1/Scripts/Tools/com.4Tey.TotheBeach.xrnx/

So maybe you can truncate the string (or what not) :)

 

You're lucky, you can probably see the sign from your window ;)



#13 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 13 August 2018 - 09:38

Ok, fair enough Raul, I can run os.currentdir from a tool.  I get:

/home/4Tey/.renoise/V3.1.1/Scripts/Tools/com.4Tey.TotheBeach.xrnx/

So maybe you can truncate the string (or what not) :)

 

You're lucky, you can probably see the sign from your window ;)

 

4Tey, I have updated my KangarooX120 tool, including the color capture discussed in this thread. Could you try this tool in Linux? The marking of objects (buttons, checkboxes, etc) should take the Selected_Button_Back color of your Renoise theme (skin). On the other hand, if the marking is white, something is wrong inside the code.

 

Thanks!  :)


:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#14 4Tey

4Tey

    Big Masta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPip
  • 555 posts
  • Gender:Male

Posted 13 August 2018 - 10:38

Could you try this tool in Linux?

Seems fine Raul (well it hasn't crashed or anything), maybe rest at ease on that deck chair (until someone finds a bug ;) )



#15 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1599 posts
  • Gender:Male
  • Location:Spain

Posted 13 August 2018 - 11:50

Seems fine Raul (well it hasn't crashed or anything), maybe rest at ease on that deck chair (until someone finds a bug ;) )

 

Thanks! So, if inside the Linux it takes the correct color of the skin, it only needs that Danoise confirms if it works correctly within MAC. Apparently, we have a good team like that:  :)

  • I (ulneiz) can try tools with Windows.
  • You (4Tey) can try tools with Linux.
  • The chief (Danoise) can try tools with Macintosh.

KangarooX120 is quite robust. I would even challenge you to find a bug that produces a crash...  :D


:excl: Development of my tools: Piano Roll EditorKangarooX120SamRenderPhraseTouch

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler





Also tagged with one or more of these keywords: string, table