Skip to content

Instantly share code, notes, and snippets.

View Buy-One's full-sized avatar
🎯
Focusing

Buy-One

🎯
Focusing
View GitHub Profile
@Buy-One
Buy-One / reverse_indexed_table.lua
Last active April 12, 2022 08:36
Reverse indexed table
function reverse_indexed_table1(t) -- around the last value
if not t then return end
for i = 1, #t-1 do -- loop as many times as the table length less 1, since the last value won't need moving
local v = t[#t-i] -- store value
table.remove(t, #t-i) -- remove it
t[#t+1] = v -- insert it as the last value
end
end
function reverse_indexed_table2(t)
@Buy-One
Buy-One / filter array values in place.lua
Last active April 28, 2022 10:14
Filter array values in place
function filter_array_vals(t, val, func) -- val can be number, string, pointer or table, of course array values type must match 'val' arg type // t arg seems unnecessary because table is recognized even being local // basic use is (in)equality evaluation; for numbers <> operators can be employed // the function works recursively
local num = type(val) == 'number'
local str = type(val) == 'string'
local ptr = type(val) == 'userdata'
local tab = type(val) == 'table'
for k, v in ipairs(t) do
if v ~= val then
-- if str and not v:match(val) then -- filtering by string contents
-- if tab and v == val[k] then -- filtering by values found in another array
table.remove(t,k)
@Buy-One
Buy-One / replace_Nth_capture_in_a_string.lua
Last active April 28, 2022 10:14
Replace Nth capture in a string (versatile)
function replace_Nth_capture(src_str, patt, repl_str, N) -- patt is either a literal string or a pattern; N is ordinal number of the capture to be replaced, if not N or 0 then N is 1
local N = N and tonumber(N) and math.abs(math.floor(N))
if not N or N == 0 then N = 1 end
local i = 1
local st, fin, capt
local capt_cnt = 0
while i < #src_str do
-- OR
--repeat
st, fin, capt = src_str:find('('..patt..')', i)
@Buy-One
Buy-One / merge 2 arrays at index.lua
Last active June 3, 2022 17:35
Merge 2 arrays at index
function merge_2_arrays_at_index(t1,t2,index) -- the result is updated t1
local offset = 1-index
for i = index, #t2+index-1 do
table.insert(t1, i, t2[i+offset])
end
end
@Buy-One
Buy-One / remove_Nth_capture.lua
Created June 13, 2022 09:46
Remove Nth capture from a string
function remove_Nth_capture(str,capt,N) -- removes with adjacent punctuation marks
-- 1. if not N then N is 1
-- 2. if no captures or N is 0 or greater than the number of captures returns original string
local N = N and tonumber(N) and math.abs(math.floor(N)) -- validate N
if not N then N = 1 end
local cntr = 0
local str_new = ''
for w1, w2 in str:gmatch('(%w*)([%p%s%c]*)') do
cntr = (N ~= cntr and w1 == capt or N == cntr) and cntr+1 or cntr
if N ~= cntr then str_new = str_new..w1..w2 end
@Buy-One
Buy-One / replace_Nth_capture.lua
Created June 13, 2022 09:48
Replace Nth capture
function replace_Nth_capture(src_str,capt,repl_str,N)
-- 1. if not N then N is 1
-- 2. if no captures or N is 0 or greater than the number of captures returns original string
-- if the 3d arg (repl_str) isn't a string then returns orig string and boolean to indicate no changes
local N = N and tonumber(N) and math.abs(math.floor(N)) -- validate N
if not N then N = 1 end
local cntr = 0
local str_new = ''
if repl_str and not type(repl_str) then return str, false end
for w1, w2 in src_str:gmatch('(%w*)([%p%s%c]*)') do
@Buy-One
Buy-One / remove_replace_Nth_capture.lua
Created June 13, 2022 09:49
Remove or replace Nth capture
function remove_replace_Nth_capture(src_str,capt,N,repl_str)
-- if the last arg is omitted or isn't a string then works for removal
-- 1. if not N then N is 1
-- 2. if no captures or N is 0 or greater than the number of captures returns original string
local N = N and tonumber(N) and math.abs(math.floor(N)) -- validate N
if not N then N = 1 end
local repl_str = repl_str and type(repl_str) == 'string' and repl_str
local cntr = 0
local str_new = ''
for w1, w2 in src_str:gmatch('(%w*)([%p%s%c]*)') do
@Buy-One
Buy-One / Convert time in seconds to a formatted string.lua
Created September 20, 2022 17:27
Convert time in seconds to a formatted string hh:mm:ss.ms
function format_time_given_in_sec(num_sec)
local function add_lead_zero(num)
return #(num..'') == 1 and '0'..num or num
end
local hrs = math.modf(num_sec/3600) -- 3600 sec in an hour
local sec = num_sec%3600 -- remainder in sec
local mnt = math.modf(sec/60) -- 60 sec in a min
local sec_ms = sec%60 -- remainder in sec and ms as a decimal part
local dec_places = 10^3
local sec_ms = math.floor(sec_ms * dec_places + 0.5) / dec_places -- round ms down to 3 dec places
@Buy-One
Buy-One / REAPERControl.ahk
Created October 6, 2022 13:30 — forked from pannal/REAPERControl.ahk
REAPER Control v0.12b AutoHotkey Script
; ----------------------------------------------------------------------------------------------------------
; _____ _____ _____ _____ _____ _____ _____ _ _
; | __ || __|| _ || _ || __|| __ | | | ___ ___ | |_ ___ ___ | |
; | -|| __|| || __|| __|| -| | --|| . || || _|| _|| . || |
; |__|__||_____||__|__||__| |_____||__|__| |_____||___||_|_||_| |_| |___||_|
;
; ----------------------------------------------------------------------------------------------------------
; REAPER Control
; panni
; 2018
@Buy-One
Buy-One / gist:dd5835675e05c69ce80b2b789385de85
Created March 13, 2023 22:09 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue: