Equal string lenght

Moderator: leecollings

Post Reply
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Equal string lenght

Post by DomoticaRob »

I want that the strings have the same lenght:

Code: Select all

--This is what I want:
--[123     ]
--[1234    ]
--[     456]
--[    3456]

--This is what I get:
--[123 ]
--[1234 ]
--[ 456]
--[ 3456]


stringOud="123"
stringNieuw = stringLengte(stringOud,6,"R")
print(stringNieuw)

stringOud="1234"
stringNieuw = stringLengte(stringOud,6,"R")
print(stringNieuw)

stringOud="456"
stringNieuw = stringLengte(stringOud,6,"L")
print(stringNieuw)

stringOud="3456"
stringNieuw = stringLengte(stringOud,6,"L")
print(stringNieuw)

function stringLengte(stringOud,lengteGewenst,kantOpvullen)
    local stringNieuw=""
    local lengteBasis=string.len(stringOud)
    local lengteRest=lengteGewenst-lengteBasis
    
    if kantOpvullen=="R" then
        stringNieuw=(stringOud..string.rep(string.char(32),lengteRest))
    elseif kantOpvullen=="L" then
        stringNieuw=(string.rep(string.char(32),lengteRest)..stringOud)
    end
    return stringNieuw
end

The problem with character 32 (space) is that in a print or io.write you don't get all the spaces you want.
I want it because I want a file output with horizontal equal distances.

What is the solution?
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Equal string lenght

Post by jvdz »

Yes they are all the same length when I run your posted test script, but you probably check it with a proportional font in stead of a fixed font like courier?
Do you want to display it in a webpage or something?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: Equal string lenght

Post by DomoticaRob »

Hey Jos,

I also have this problem in the Domoticz log.

Unfortunately I can't attach pictures anymore to this forum, I get this error code:

ERROR
Sorry, the board attachment quota has been reached.


Otherwise I could show you a screendump.

When I use for example character 46, that is a dot [.] then it's ok. So all the distances are equal. But not with character 32 [space].
The output of my script is a textfile. When I use it in a texteditor with non-proportional font I also don't get equal distances.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Equal string lenght

Post by waaren »

DomoticaRob wrote: Tuesday 18 December 2018 23:54 I also have this problem in the Domoticz log.
/quote]
Tested your code in a dzVents script (output to log) and I see the expected result.

Code: Select all

2018-12-19 01:39:31.622  Status: dzVents: [123   ]
2018-12-19 01:39:31.622  Status: dzVents: [1234  ]
2018-12-19 01:39:31.622  Status: dzVents: [   456]
2018-12-19 01:39:31.622  Status: dzVents: [  3456]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Equal string lenght

Post by jvdz »

DomoticaRob wrote: Tuesday 18 December 2018 23:54 Hey Jos,
I also have this problem in the Domoticz log.

When I use for example character 46, that is a dot [.] then it's ok. So all the distances are equal. But not with character 32 [space].
The output of my script is a textfile. When I use it in a texteditor with non-proportional font I also don't get equal distances.
So you are looking at the Webpage that displays the Domoticz log? Html will not display multiple spaces unless they are   (None breaking spaces)
When you look at the actual domoticz,log file with an editor that uses a none proportional font, you will see they are there. :)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: Equal string lenght

Post by DomoticaRob »

You're damn right.

Problem solved.

Thanks everybody.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest