mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Hi all,

I have a simple .ini file in the following format:
Code
[Alpha]
some_text="foo "

The .ini file is located in the same folder as mirc.exe. In my script I access it using %my_ini

Now, when I try to read the value of the key some_text from the ini using the following code...
Code
%var $readini(%my_ini, Alpha, some_text)
...the result is foo without the trailing space.

Also using $len(%var) in my script returns 3 ("foo") instead of 4 ("foo ").

That being said, my very basic question is:
How do I get the unchanged value with all its trailing and/or even leading spaces? Without the quotes of course.
And how to I work with it later in my code, for example when getting its lenght by using $len as mentioned above?


Thanks in advance! smile

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Use the n switch of $readini and use /var -p


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2012
Posts: 299
Fjord artisan
Offline
Fjord artisan
Joined: Jan 2012
Posts: 299
I don't know how much this method will suit you, but you can try using an invisible character instead of a space, which looks like a space, which will definitely count as 1 character unit.
Just hold down the "ALT" key and press the numbers on the keyboard on the right "0160" and you will get the desired character.

Or you can run this command and get this symbol which will be between quotes:
Code
//echo -a 160 = $+(",$chr(160),")

This would be the simplest solution, because in your case the space "$chr(32)" will not read as 1 character unit.



🌐 http://forum.epicnet.ru 📜 irc.epicnet.ru 6667 #Code | mIRC scripts, help, discuss, examples
Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
Your problem has 2 hurdles to jump through. /writeini can't support writing trailing spaces to disk unless you enclose your written string with doublequotes. However $readini won't read the surrounding doublequotes nor will it read any leading/trailing spaces. There is a workaround by reading it into a hashtable, from where you can fetch the value using $hget.

Your 2nd problem is that /var and /set will allow setting trailing spaces only if there's at least 2 of them, unless you use the -p switch. In the example below, it shows that the text file contains a leading and trailing space in the 'data', but $readini can only fetch the 4 characters of 'data' without the leading and trailing spaces. However when loading it into a hashtable using /hload, the hashtable item does have the doublequotes stripped, but it does preserve the leading/trailing spaces. Note that using the -p switch with %a1 has no effect because there is no trailing space fetched from disk, but if you remove the -p when setting %a2, you'll lose that trailing space and the length drops from 6 to 5.

//remove test.ini | writeini -n test.ini section item " data " | echo -a $qt($read(test.ini,nt,2)) length read by readini is $len($readini(test.ini,section,item)) | var -sp %a1 $readini(test.ini,section,item) | hload -im test test.ini section | var -sp %a2 $hget(test,item) | echo -a a1 %a1 $len(%a1) vs a2 %a2 $len(%a2)

Note that if you create a hashtable item containing the doublequotes, the quotes are seen by $hget, and they're written to disk by hsave, but they're stripped by /hload.

If you want to create a hashtable item containing 1 trailing space, you'd need to put it into a &binvar then create the table item using /hadd -b

//var -sp %a $+(data,$chr(32)) | noop $regsubex(foo,$+(data,$chr(32)),,,&var) | echo -a $bvar(&var,1-) | hadd -bm table item &var | echo -a $len($hget(table,item))

Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Thanks all.

Originally Posted by Wims
Use the n switch of $readini and use /var -p
This seems to do the job so far smile


However if the ini file does not contain the key some_text,
I need to set it manually to (my default value) "foo " in my script code.
How do I do this please? Here is my code snippet:
Code
on *:INPUT:*:{
  [...]
  var -p %value = $readini(%my_ini,n,Alpha,some_text)
    if (%value == $null) { %value = foo $+ $char(32) }
    echo -a %value
    var %valueLen = $len(%value)
    if (%value != $left($1-,%valueLen)) {
      >> do some magic here <<
  }
}

So when the key can't be read because it does not exist in the .ini, I try to set it manually using
if (%value == $null) { %value = foo $+ $char(32) }
When I do a /echo -a %value after setting it, it says "foo" instead of the expected "foo ", also the following lenght check fails.

What am I doing wrong? confused

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
When you do "%var = value", it is an implicit /set, you must use the explicit syntax var or set -p %value = foo $+ $chr(32)


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard