Jump to content




How to put comas between valors in a function


  • You cannot reply to this topic
12 replies to this topic

#1 astonish01

  • Members
  • 33 posts

Posted 17 August 2014 - 01: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 by astonish01, 17 August 2014 - 02:12 PM.


#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 August 2014 - 02: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?

#3 astonish01

  • Members
  • 33 posts

Posted 17 August 2014 - 02:10 PM

View PostMKlegoman357, on 17 August 2014 - 02:00 PM, said:

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 by astonish01, 17 August 2014 - 02:11 PM.


#4 sEi

  • Members
  • 41 posts

Posted 17 August 2014 - 02:15 PM

Is it something like this you think of?
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 by sEi, 17 August 2014 - 02:19 PM.


#5 astonish01

  • Members
  • 33 posts

Posted 17 August 2014 - 02:24 PM

View PostsEi, on 17 August 2014 - 02:15 PM, said:

Is it something like this you think of?
a = 14
b = 54
c = 25
str = a..", "..b..", "..c
print(str)
RESULT
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 by astonish01, 17 August 2014 - 02:27 PM.


#6 sEi

  • Members
  • 41 posts

Posted 17 August 2014 - 02: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

#7 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 17 August 2014 - 02:55 PM

View PostsEi, on 17 August 2014 - 02:29 PM, said:

-snip-

I have been working on this and if the OP waits an hour or two I will post a solution.

#8 astonish01

  • Members
  • 33 posts

Posted 17 August 2014 - 03:03 PM

View PostGamerNebulae, on 17 August 2014 - 02:55 PM, said:

View PostsEi, on 17 August 2014 - 02:29 PM, said:

-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)

#9 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 17 August 2014 - 04:39 PM

Here's the function:

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 by GamerNebulae, 18 August 2014 - 12:08 PM.


#10 Anavrins

  • Members
  • 775 posts

Posted 17 August 2014 - 05: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 by Anavrins, 18 August 2014 - 09:50 PM.


#11 astonish01

  • Members
  • 33 posts

Posted 17 August 2014 - 07:13 PM

Thx guys, both worked well

#12 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 17 August 2014 - 08:04 PM

View PostAnavrins, on 17 August 2014 - 05:56 PM, said:

-snip-

Yeah, I never understood the string manipulation of Lua well.

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 August 2014 - 02:09 AM

There is a simpler to understand pattern to solve this solution.
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
knowing this we can see that the pattern in that code works by saying:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users