This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How to put comas between valors in a function
Started by astonish01, 17 August 2014 - 12:49 PMPosted 17 August 2014 - 02:49 PM
It's like. i define the function, and i say a computer to print it, but i want to put comas between the numbers
Edited on 17 August 2014 - 01:12 PM
Posted 17 August 2014 - 03:00 PM
What? Could you explain it a little bit more? You are talking about how to print a function, and put comas between the numbers? What numbers? Could you show us an example?
Posted 17 August 2014 - 03:10 PM
What? Could you explain it a little bit more? You are talking about how to print a function, and put comas between the numbers? What numbers? Could you show us an example?
im going to make a example with energy cells,
cell = peripheral.wrap("side")
monitor = peripheral.wrap("side")
while true do
energy = cell.getEnergyStored("direction")
monitor.clear()
monitor.setCursorPos("number,number)
m.write(energy.." ")
sleep(0.5)
end
i may have said function and it is variable and you may thought print is printing in a printer, but with print i meant the monitor thing. (if so, this helps)
Edited on 17 August 2014 - 01:11 PM
Posted 17 August 2014 - 03:15 PM
Is it something like this you think of?
Hope this helps
/sEi
EDIT: I was writing my answer before you posted the post above. Seing the post i am even more confused in what you try to do!
a = 14
b = 54
c = 25
str = a..", "..b..", "..c
print(str)
RESULT
14, 54, 25
Hope this helps
/sEi
EDIT: I was writing my answer before you posted the post above. Seing the post i am even more confused in what you try to do!
Edited on 17 August 2014 - 01:19 PM
Posted 17 August 2014 - 03:24 PM
Is it something like this you think of?RESULTa = 14 b = 54 c = 25 str = a..", "..b..", "..c print(str)
14, 54, 25
Hope this helps
/sEi
yeah, but its like, i just have energy, and its one valor, the energy is never still, so it needs to keep reading the energy levels, so the number would be 50000000, but after some usage the energy would be 25000000, and i want to do 25,000,000 (of course when its full it will be 5,000,000)
so i don't know how to divide energy(as in the code) in diferent values to put them between the comas, and also, if the energy drops to 7 characters or 6, is there a way to remove the extra coma tht will be there? (like, it drops to 050,000 instead of 00,050,000 and after it comes back to 50,000,000)
Edited on 17 August 2014 - 01:27 PM
Posted 17 August 2014 - 03:29 PM
Ok, now i understand your question. I could come up with a rude way to do it BUT i bet there is a smart way to do it and hope someone will answer your question soon.
/sEi
/sEi
Posted 17 August 2014 - 03:55 PM
-snip-
I have been working on this and if the OP waits an hour or two I will post a solution.
Posted 17 August 2014 - 04:03 PM
-snip-
I have been working on this and if the OP waits an hour or two I will post a solution.
well, i have all the year probably (or till direwolf20 goes to 1.7/8 public)
Posted 17 August 2014 - 05:39 PM
Here's the function:
EDIT: fixed a bug where numbers beyond one billion weren't readable (commas in wrong places)
function comma(text)
local textTable = {}
for char in string.gmatch(text, ".") do
textTable[#textTable + 1] = char
end
local commaCount = math.floor(#text / 3)
if (#text % 3 == 0) then
commaCount = commaCount - 1
end
local base = #textTable - 2
for i = 0, commaCount - 1 do
table.insert(textTable, (base - (i * 3)), ",")
end
local commaText = ""
for k,v in pairs(textTable) do
commaText = commaText..v
end
return commaText
end
EDIT: fixed a bug where numbers beyond one billion weren't readable (commas in wrong places)
Edited on 18 August 2014 - 11:08 AM
Posted 17 August 2014 - 06:56 PM
Smaller function
local function comma_value(n)
local left, num, right = n:match("^([^%d]*%d)(%d*)(.-)$")
return left..(num:reverse():gsub("(%d%d%d)","%1,"):reverse())..right
end
Edited on 18 August 2014 - 08:50 PM
Posted 17 August 2014 - 08:13 PM
Thx guys, both worked well
Posted 17 August 2014 - 09:04 PM
-snip-
Yeah, I never understood the string manipulation of Lua well.
Posted 18 August 2014 - 03:09 AM
There is a simpler to understand pattern to solve this solution.
Give me all the numbers, except for the last three, from the start of the sting into the first capture, and the last three numbers into the second capture. then combine them back together with a comma separating them.
function comma_value( num )
local formatted = num
repeat
formatted, replaced = formatted:gsub("^(-?%d+)(%d%d%d)", '%1,%2')
until replaced == 0
return formatted
end
You can take a look at the PIL on patterns to see what parts do, however the basic breakdown is- ^ from the start of the string
- () capture these characters
- ? the previous character/match is optional
- %d match a decimal numbers
- + one or more repetition
- %n the nth capture, i.e. %1 is the first capture
Give me all the numbers, except for the last three, from the start of the sting into the first capture, and the last three numbers into the second capture. then combine them back together with a comma separating them.