gawk: change 5th element on every other line - printing

I want to add a line break (\n) in front of the 5th element on every other line:
2, 0, 0, 0, 2
4, 0, 0, 0, 4
6, 0, 0, 0, 6
8, 0, 0, 0, 8
... in order to get:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
What I have so far in gawk doesn't work:
gawk '{if (NR % 2) {$5=\n$5; print} else print}'

You could say:
awk '{NR%2 || $5="\\n"$5 }1' filename
Note that you'll need to escape the \ in order to get a literal \.
For your input, it'd produce:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
Alternatively, (as pointed out by #WilliamPursell), you could say:
awk '!(NR%2) {$5="\\n"$5 }1' filename

Related

how to parse strings from a file?

I have a text file.
each block parameter is separated by a colon
WoodBlock : 0.886275, 0.607843, 0.25098 : 2, 2, 2 : true : -75.5656891, -11.899992, -416.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
RustedBlock : 0.639216, 0.635294, 0.647059 : 2, 2, 2 : true : -35.5656891, -11.899992, -424.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
StoneBlock : 0.639216, 0.635294, 0.647059 : 2, 2, 2 : true : -50.5656891, -11.899992, -425.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
MetalRod : 0.388235, 0.372549, 0.384314 : 1, 3, 1 : true : -51.5656891, -11.399992, -412.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
Each line contains information about a block. I want to make an algorithm that will work like this:
the first line is selected
the second parameter of the line is selected - the script does something with this parameter
the third parameter of the line is selected - the script does something with this parameter
the fourth parameter of the line is selected - the script does something with this parameter
select the fifth parameter of the line - the script does something with this parameter
etc.
I tried using gsub but I don't know how I can select a certain line or parameter in it
You can use string.gmatch to match all parameters in the colon-delimited string. The pattern used to indicate the separator is [^:]+, which translates to "match everything but : (a colon)".
Here's an example script that loops over each line, then prints each parameter.
for line in io.lines("blocks.txt") do
for param in string.gmatch(line, "[^:]+") do
print(param)
end
end

Logitech Lua help needed

software = Logitech G hub
UPDATE
Okay! Thanks to some help this is where I'm at, Everything's working very nicely and as it should but now I face a new task and that is adding variations of each table for the slight changes to recoil when using scopes.
I could simple add more gun modes and make the changes required but I'd really like to make a way of using a key to toggle whether they are used or not, An idea I have floating around my head is for F9 and F10 to toggle between the scopes and when the toggle is off it will work with the code I already have. however I'm not sure this is possible, to my understanding I could use the lock keys (capslock, scrolllock, numlock) to make this work. initially I had the idea to have a toggle key that modifies the timing of the recoil sequence but unfortunately this is not an accurate solution.
What I'd like to do now is create 2/3 toggles that allow gun modes to switch between variable tables without having to add more guns
any help is greatly appreciated and that goes for any help I've received so far too
thanks
---GUN MODES---
local GUN1_Mouse = 8
local GUN1_Keyboard = nil
local GUN2_Mouse = 7
local GUN2_Keyboard = nil
local GUN3_Mouse = 9
local GUN3_Keyboard = nil
local GUN4_Mouse = nil
local GUN4_Keyboard = nil
local GUN5_Mouse = 5
local GUN5_Keyboard = nil
local GUN6_Mouse = nil
local CUSTOM_Keyboard = nil
local GUN7_Mouse = 4
local GUN7_Keyboard = nil
---RECOIL TABLES---
local recoil_table = {}
-------------------------------------
recoil_table["GUN1"] = {
0, 6, 133,
-1, 6, 133,
-1, 6, 133,
-4, 6, 133,
-3, 6, 133,
-3, 6, 133,
-3, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
}
recoil_table["GUN1_crouch"] = {
0, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-3, 3, 133,
-3, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
}
-------------------------------------
recoil_table["GUN2"] = {
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
}
recoil_table["GUN2_crouch"] = {
0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
}
-------------------------------------
recoil_table["GUN3"] = {
0, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-3, 3, 133,
-3, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
}
recoil_table["GUN3_crouch"] = {
0, 2.5, 125,
0, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.8, 2.5, 125,
0.9, 2.5, 125,
1, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.8, 2.5, 125,
0.9, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
}
-------------------------------------
recoil_table["GUN4"] = {
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
}
recoil_table["GUN4_crouch"] = {
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
}
-------------------------------------
recoil_table["GUN5"] = {
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
}
recoil_table["GUN5_crouch"] = {
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
}
-------------------------------------
recoil_table["GUN6"] = {
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
}
recoil_table["GUN6_crouch"] = {
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
}
-------------------------------------
recoil_table["GUN7"] = {
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
}
-------------------------------------
recoil_table["GUN7_crouch"] = {
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
}
---------------------------------------------------
local weapon
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN1_Mouse) or (event == "G_PRESSED" and arg == GUN1_Keyboard) then
weapon = weapon ~= "GUN1" and "GUN1"
if not weapon then
OutputLogMessage("OFF-GUN1\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN1\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN2_Mouse) or (event == "G_PRESSED" and arg == GUN2_Keyboard) then
weapon = weapon ~= "GUN2" and "GUN2"
if not weapon then
OutputLogMessage("OFF-GUN2\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN2\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN3_Mouse) or (event == "G_PRESSED" and arg == GUN3_Keyboard) then
weapon = weapon ~= "GUN3" and "GUN3"
if not weapon then
OutputLogMessage("OFF-GUN3\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN3\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN4_Mouse) or (event == "G_PRESSED" and arg == GUN4_Keyboard) then
weapon = weapon ~= "GUN4" and "GUN4"
if not weapon then
OutputLogMessage("OFF-GUN4\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN4\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN5_Mouse) or (event == "G_PRESSED" and arg == GUN5_Keyboard) then
weapon = weapon ~= "GUN5" and "GUN5"
if not weapon then
OutputLogMessage("OFF-GUN5\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN5\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN6_Mouse) or (event == "G_PRESSED" and arg == GUN6_Keyboard) then
weapon = weapon ~= "GUN6" and "GUN6"
if not weapon then
OutputLogMessage("OFF-GUN6\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN6\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN7_Mouse) or (event == "G_PRESSED" and arg == GUN7_Keyboard) then
weapon = weapon ~= "GUN7" and "GUN7"
if not weapon then
OutputLogMessage("OFF-GUN7\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN7\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
---------------------------------------------------
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and weapon and IsMouseButtonPressed(3) then
local recoil_sequence = recoil_table[weapon..(IsModifierPressed("lctrl") and "_crouch" or "")] or {}
local x, y, tm = 0, 0, GetRunningTime()
for j = 3, #recoil_sequence, 3 do
local dx_10ms = recoil_sequence[j-2]
local dy_10ms = recoil_sequence[j-1]
local duration = recoil_sequence[j]
repeat
Sleep(10)
if not IsMouseButtonPressed(1) then break end
local dt = math.min(GetRunningTime() - tm, duration)
duration, tm = duration - dt, tm + dt
x, y = x + dt/10 * dx_10ms, y + dt/10 * dy_10ms
local int_x, int_y = math.floor(x), math.floor(y)
x, y = x - int_x, y - int_y
MoveMouseRelative(int_x, int_y)
until duration == 0
if not IsMouseButtonPressed(1) then break end
end
end
end
You should use function GetRunningTime() to check if it is time to change recoil direction.
---GUN MODES---
local AK47_Mouse = 8
local AK47_Keyboard = nil
------------- for adding another weapon -----------
local AnotherWeapon_Mouse = 7
local AnotherWeapon_Keyboard = nil
---------------------------------------------------
local SpeedX2Modifier_Mouse = nil
local SpeedX2Modifier_Keyboard = 1 -- key G1
local SpeedX2 = false
---RECOIL TABLES---
local recoil_table = {}
recoil_table["AK47"] = {
-- down for 1 second
0, 0.5, 1000, -- delta_x_per_10ms, delta_y_per_10ms, duration_in_ms
-- diagonal for 1 second
-2, 4, 1000,
-- it stops moving after 2 seconds (even if LMB is still pressed)
}
recoil_table["AK47_crouch"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
recoil_table["AK47_reddot"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
------------- for adding another weapon -----------
recoil_table["AnotherWeapon"] = {
-- down for 1 second
0, 0.5, 1000, -- delta_x_per_10ms, delta_y_per_10ms, duration_in_ms
-- diagonal for 1 second
-2, 4, 1000,
-- it stops moving after 2 seconds (even if LMB is still pressed)
}
recoil_table["AnotherWeapon_crouch"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
recoil_table["AnotherWeapon_reddot"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
---------------------------------------------------
local weapon
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == AK47_Mouse) or (event == "G_PRESSED" and arg == AK47_Keyboard) then
weapon = weapon ~= "AK47" and "AK47"
if not weapon then
OutputLogMessage("OFF-Macro-AK47\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-NoRecoil-AK47\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == AnotherWeapon_Mouse) or (event == "G_PRESSED" and arg == AnotherWeapon_Keyboard) then
weapon = weapon ~= "AnotherWeapon" and "AnotherWeapon"
if not weapon then
OutputLogMessage("OFF-Macro-AnotherWeapon\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-NoRecoil-AnotherWeapon\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
---------------------------------------------------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == SpeedX2Modifier_Mouse) or (event == "G_PRESSED" and arg == SpeedX2Modifier_Keyboard) then
SpeedX2 = not SpeedX2
if not SpeedX2 then
OutputLogMessage("OFF-SpeedX2\n")
else
OutputLogMessage("ON-SpeedX2\n")
end
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and weapon and IsMouseButtonPressed(3) then
local recoil_sequence = recoil_table[weapon..(
IsKeyLockOn("capslock") and "_crouch" or
IsKeyLockOn("scrolllock") and "_reddot" or "")] or {}
local x, y, tm = 0, 0, GetRunningTime()
for j = 3, #recoil_sequence, 3 do
local dx_10ms = recoil_sequence[j-2]
local dy_10ms = recoil_sequence[j-1]
if SpeedX2 then
dx_10ms = dx_10ms * 2
dy_10ms = dy_10ms * 2
end
local duration = recoil_sequence[j]
repeat
Sleep(10)
if not IsMouseButtonPressed(1) then break end
local dt = math.min(GetRunningTime() - tm, duration)
duration, tm = duration - dt, tm + dt
x, y = x + dt/10 * dx_10ms, y + dt/10 * dy_10ms
local int_x, int_y = math.floor(x), math.floor(y)
x, y = x - int_x, y - int_y
MoveMouseRelative(int_x, int_y)
until duration == 0
if not IsMouseButtonPressed(1) then break end
end
end
end

Getting Sum & Average from response coming from Server

I am iOS developer & I have JSON file in my project. I am getting below JSON array from a file saved as below
["fa", "pinfield", [
[0, 0, 26082730, ["pt", 213.3332769870769, 584.3332864443584, 414, 226]],
[1, 0, 26082819, ["pt", 0, 0, 414, 226]]
]],
["fa", "pinfield", [
[0, 0, 26084394, ["pt", 195.999944249791, 529.3332950671626, 414, 226]],
[1, 0, 26084475, ["pt", 0, 0, 414, 226]]
]],
["fa", "pinfield", [
[0, 0, 26082219, ["pt", 221.3332726558057, 555.9999453624226, 414, 226]],
[1, 0, 26082310, ["pt", 0, 0, 414, 226]]
]],
["fa", "pinfield", [
[0, 0, 26083275, ["pt", 200.3332853317256, 641.3332811991493, 414, 226]],
[1, 0, 26083364, ["pt", 0, 0, 414, 226]]
]],
["fa", "pinfield", [
[0, 0, 26083755, ["pt", 224.3332901000954, 705.3332707087325, 414, 226]],
[1, 0, 26083836, ["pt", 0, 0, 414, 226]]
]]
There are five objects in this array & each array have sub-arrays.
Now is there any way to calculate the average of above values & put into a signle array as below
["fa", "pinfield", [
[0, 0, xxx, ["pt", xxx.xxxxx, xxx.xxxxx, xxx.xxxxx, xxx.xxxxx]],
[1, 0, xxx, ["pt", 0, 0, xxx.xxxxx, xxx.xxxxx]]
]]
I can run for loops but it will be a time taking approach. Any help would be great.
At some level solving this problem will involve looping through your array of data, building sum and average values.
You could use the Swift reduce higher-order function to build a sum of all of the other values, and then calculate the average in a final step.
An example of using reduce to calculate the sum and average of a simpler array:
let array = [1, 7, 14, 35, 14, 12, 17]
let sum = array.reduce(0, +)
let average = Double(sum) / Double(array.count)
print("Sum = \(sum), avrage = \(average)")

Scanning an Array for String Matches in Ruby

I have an array that looks like this:
foo = ["25\"/64cm", "0\"/0cm", "0\"/0cm", "0\"/0cm", "2\"/6cm", "0\"/0cm", "0\"/0cm", "0\"/0cm", "0\"/0cm", "0\"/0cm"]
I am trying to scan the array, and return a new array of the values (integers) between the / and cm. I am hoping it would come out at:
bar = [64, 0, 0, 0, 6, 0, 0, 0, 0, 0]
Sorry I am new to ruby
foo.map {|s| s[/\d+cm/].to_i }
# => [64, 0, 0, 0, 6, 0, 0, 0, 0, 0]
There should be at least one answer that doesn't use a regex:
foo.map { |s| s.split('/').last.to_i }
#=> [64, 0, 0, 0, 6, 0, 0, 0, 0, 0]
foo.map { |s| /(\d+)cm/.match(s)[1].to_i }
Try This:
foo.join().scan(/\b(\d+cm)/).flatten.map{|x| x.to_i}

sqlite with iOS 5 issue

OK this is the case, I have the following query
INSERT INTO 'FoodListTBL' ('AutoNo','CHOCals','PrtCals', 'FatCals','CHOgram','PrtGram','FatGram','CatId', 'TimeTypeId','TotalCals','Visibl', 'IsActive','NameAr','NameEn','CountryId', 'TotalPerUnit','UnitId','PreferedBread1', 'PreferedMilk1','PreferedVeg1','PreferedFat1', 'PreferedFruit1','CauseAllergy','AllergyCatId', 'TotalLikes','NameDescEn','NameDescAr','ChoPerUnit','PrtPerUnit','FatPerUnit','Quantity','PreferedBread2','PreferedMilk2', 'PreferedVeg2','PreferedFat2', 'PreferedFruit2','IV','UV','InsertDate','InsertUser') VALUES (818,0,0, 45, 0, 0, 5, 17, 1, 45,1, 1, 'زبدة قليلة الدسم', 'Butter reduced fat', 0, 45, 14, 0, 0, 0, 0, 0, 0, 0, 0, 'Butter reduced fat', 'زبدة قليلة الدسم', 0, 0, 45, 1, 0, 0, 0, 0, 0, 492, 0, '-', '-' ),(819,0,0, 45, 0, 0, 5, 17, 1, 45,1, 1, 'زبدة', 'Butter regular', 0, 45, 4, 0, 0, 0, 0, 0, 0, 0, 0, 'Butter regular', 'زبدة', 0, 0, 45, 1, 0, 0, 0, 0, 0, 493, 1475, '-', '-')
this query executed successfully on iOS 6.X and failed on any iOS less than 5.X taking into consideration that any other insert query on other tables finished successfully on any iOS
and I've tried two codes for insert this is one of them
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSLog( #"Error while inserting data: '%s'", sqlite3_errmsg(database));
else NSLog(#"New data inserted");
sqlite3_reset(compiledStatement);
}else
{
NSLog( #"Error while inserting '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement)
and the result in the two cases is
Error while inserting 'near ",":syntax error'
aging this query is functional on every thing except iOS < 6.0
any clues are appreciated
SQLite before version 3.7.11 does not support the multi-record INSERT syntax.
Use multiple INSERT commands, or insert the records with INSERT ... SELECT ... UNION ALL ....

Resources