Problem with String.Count[]

Need help with a macro you are writing? Ask here!

Moderator: MacroQuest Developers

chawk
orc pawn
orc pawn
Posts: 14
Joined: Sat May 08, 2004 4:08 am

Problem with String.Count[]

Post by chawk » Sat May 08, 2004 4:22 am

edit: on second thought, I should download Service Pack 6 first then see if this bug still exists :shock:
edit2: still freezes

In a script I'm writing which auto-invites people to a channel based on names in an .ini file, I used String.Count[] and it froze EQ each time:

Code: Select all

Sub LoadInviteList
    /declare ListSize int 0
    /declare ListData string

    /varset ListData ${Ini[InviteList.ini,names,,]}
    /echo ${ListData.Count[|]} occurences
    
    | more code follows...

/return
I was basically trying to count the number of entries in the .ini file by using Ini[], which I tested returned a string of a format "key|key|key||" when I omitted a key parameter and just passed a section. I wanted to count the number of | characters to calculate the number of entries in a section.

My first thought was that maybe the | character was causing a parsing error since it's a comment delimiter, but then I tried:

Code: Select all

/echo ${String[Hello].Count[l]}
.. which should return 2 (2 L's in Hello?) but that froze EQ too. Anyone else having this problem?

edit: an addendum to this, my initial method of reading a variable-line-length .ini file was using String.Arg[] until it returned NULL but I'm not sure how to test if a string variable is NULL. How do I do that?
Last edited by chawk on Sat May 08, 2004 8:29 am, edited 2 times in total.

Preocts
a snow griffon
a snow griffon
Posts: 312
Joined: Thu Jan 29, 2004 1:02 pm

Post by Preocts » Sat May 08, 2004 6:59 am

Well first off this is more of a bug report than anything but Lax will see it none-the-less.

Should work the way you were doing it. Quick glance at MQ yeilds me to think the loop that counts the number of occurances doesn't stop. But then, my understanding of C is still limited.

chawk
orc pawn
orc pawn
Posts: 14
Joined: Sat May 08, 2004 4:08 am

Post by chawk » Sat May 08, 2004 7:11 am

I had a feeling it might have been a bug with the Count[] function but I was making sure I was using it right first. Guess I'll find a different method for now. My original problem was testing if a string variable was NULL, how do I do that? :P

Preocts
a snow griffon
a snow griffon
Posts: 312
Joined: Thu Jan 29, 2004 1:02 pm

Post by Preocts » Sat May 08, 2004 7:15 am

Umm. Depends. Easiest way I do it is:

Code: Select all

  /declare SomeVar string local 
  /if (!${SomeVar.Length}) /echo there is nothing there.

chawk
orc pawn
orc pawn
Posts: 14
Joined: Sat May 08, 2004 4:08 am

Post by chawk » Sat May 08, 2004 7:59 am

That seems like it will work for my intents. Depends on how equally a 0 will evaluate to NULL, functionally too. In the case of functions that return ints, I'm sure in some cases, a 0 is a valid result whereas NULL could be indicative of some other error. Either way, I'll try that, thanks.

As for the bug, it indeed seems to be one. I got brave and peeked around in the source. Code snippet for Count[]:

Code: Select all

	case Count:
		if (Index[0])
		{
			Dest.DWord=0;
			PCHAR pLast=(PCHAR)VarPtr.Ptr;
			/*
			while(pLast=strchr(pLast,Index[0]))
				Dest.DWord++;
			*/
			while(*pLast)
			{
				if (*pLast == Index[0]) Dest.DWord++;
				pLast++;
			}
			Dest.Type=pIntType;
			return true;
		}
		return false;
I commented out the existing while loop and wrote the newer one, compiled, and it worked. The test in my previous post, ${String[Hello].Count[l]} evaluates to 2 as it should. Guess this should go the Bug report forum? First time posting here :P

Preocts
a snow griffon
a snow griffon
Posts: 312
Joined: Thu Jan 29, 2004 1:02 pm

Post by Preocts » Sat May 08, 2004 8:37 am

I'm sure in some cases, a 0 is a valid result whereas NULL could be indicative of some other error. Either way, I'll try that, thanks.
Any errors in ${} will return NULL. If you wanted, just do a check to make sure that var actually existed in the first place. ${Defined[VarName]}.