Fixed length string - number begin with a zero SORTED - closed

I was wondering if there was a way of doing this on the fly so to speak instead of having to check each one individual.

I would like to create an array with 126 elements all being calculated from the initial xhex and yhex, the only problem I think I will have is setting the format to include the first zero if any, or will I have to end up checking each one. That’s an extra 10 lines to each array, or will I be able to create a function to do the checking for me.

This is what I have found out so far


-- number

-- Use this function to perform your initial setup
function setup()
    xhex = 02
    yhex = 06
    
    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)
    -- Do your drawing here
    
    if string.len(xhex)~=2 then
        xxhex = string.format("%#o",xhex)
    else
        xxhex = xhex
    end
    if string.len(yhex)~=2 then
        yyhex = string.format("%#o",yhex)
    else
        yyhex = yhex
    end
    print (xxhex)
    print (yyhex)
    print (xxhex.." "..yyhex)
end

Any pointers would be helpful

Regards

@Jazmal You’re almost correct. The 2 means two positions, the 0 means fill with leading 0’s. The d is for integer. So if I said %06d, it would print a 2 as 000002. You could find out more info by searching for string.format. There are a lot moe options for it.

Or maybe this example… Is the string supposed to be “hex” values. If they are, you could use “%02x”.


function setup()
    str=""
    
    xhex=2
    yhex=6
    test(xhex,yhex)
    
    xhex=9
    yhex=34
    test(xhex,yhex)
    
    print(str)    
end

function test(x,y)
    str=str..string.format("%02d %02d ",x,y)
end

@dave1707 + loads

That did the trick but could you explain the formula

I would guess the 02 in “%02d” indicate two places of zero ? And the d for numbers

Thanks

Not hex value as such but hex as in a hexagon shape starting in the center and increasing each circle so 1 in the centre and 6 round that and 12 up to the 126. I can use the http to give me a nicer hex map but I want to try and do it myself, wont look as good but it will make me feel better

@Jazmal Is this what you’re trying to do.


function setup()
    xhex = 02
    yhex = 06

    a=string.format("%02d",xhex)
    b=string.format("%02d",yhex)
    
    print(a.." "..b)
end