Cool VL Viewer forum

View unanswered posts | View active topics It is currently 2025-05-12 03:47:55



Reply to topic  [ 7 posts ] 
What am I doing wrong? string.find 
Author Message

Joined: 2024-05-09 06:01:01
Posts: 8
Reply with quote
(I am using Cool VL Viewer v1.32.0.24, May 25 2024 10:08:48)

I have looked at Henri's scripts and don't see anything different from how I am using string.find

I have reduced my Lua script to just enough to show what is happening for me.

Code:
function OnScriptDialog(dialog_id, message, buttons)
    print("Dialog: ", message)
    local i, j = string.find(message, "Max", 1, true)
    print("Find: ", i, j)
end


This is what I see in the chat window:

Code:
[21:39:13 PDT]  Lua: Dialog:
[21:39:13 PDT]  Lua: Dialog test
Push the Button, Max
[21:39:13 PDT]  Lua: Find:
[21:39:13 PDT]  Lua: nil
[21:39:13 PDT]  Lua: nil


The message clearly has the string of interest in it.

If I specify a pattern of just "M", it works, but in my actual script, I will need a whole word.


2024-05-26 06:52:26
Profile

Joined: 2009-03-17 18:42:51
Posts: 5984
Reply with quote
Your usage of Lua string.find() is simply incorrect. You should use:
Code:
local i, j = string.find(message, "Max")


2024-05-26 07:57:18
Profile WWW

Joined: 2024-05-09 06:01:01
Posts: 8
Reply with quote
Henri Beauchamp wrote:
You should use:
Code:
local i, j = string.find(message, "Max")


That's what I first tried and got the same result. I added the optional arguments to see if making it a "plain" find (instead of regex) per the Lua 5.4 reference (https://www.lua.org/manual/5.4/manual.h ... tring.find)


2024-05-26 09:02:16
Profile

Joined: 2009-03-17 18:42:51
Posts: 5984
Reply with quote
Verify that you did not use UTF-8 characters in your dialog message (since you do not give the LSL script sources here, it is hard to find what is the problem); for example, are the "a" or the " x" of "Max" actual ASCII characters or are they some fancy UTF-8 variation ?...

Since your code works for finding "M" and not when trying to find "Max", there might be an issue with how the latter is spelled in your LSL script... Oh, and also verify that your Lua script is saved as ASCII/ISO/ANSI (i.e. 8 bits characters only) and not as UTF-8 (or, worst, UTF-16) by the editor you are using to write/edit it !

I verified here that your Lua code does work with the following LSL script:
Code:
default {
    touch_start(integer n) {
        llDialog(llDetectedKey(0), "Dialog test\nPush the Button, Max", [ "Max", "Min" ], -1);
    }
}


2024-05-26 09:13:14
Profile WWW

Joined: 2024-05-09 06:01:01
Posts: 8
Reply with quote
Henri Beauchamp wrote:
Verify that you did not use UTF-8 characters in your dialog message (since you do not give the LSL script sources here, it is hard to find what is the problem); for example, are the "a" or the " x" of "Max" actual ASCII characters or are they some fancy UTF-8 variation ?...

I checked that and as best I can tell it not UTF-8 or other fancy characters.
Quote:
Since your code works for finding "M" and not when trying to find "Max", there might be an issue with how the latter is spelled in your LSL script... Oh, and also verify that your Lua script is saved as ASCII/ISO/ANSI (i.e. 8 bits characters only) and not as UTF-8 (or, worst, UTF-16) by the editor you are using to write/edit it !

I made sure my Lua script is ANSI, but that did not help.

I tried printing the message using print(string.format("%q", message)) and did not see anything fancy.

I am now working on printing the message as a series of byte values.

(I am new to Lua, though not programming. Usually I program in C.)


2024-05-27 06:35:20
Profile

Joined: 2009-03-17 18:42:51
Posts: 5984
Reply with quote
The only thing I can think about is an issue in your LSL script (please, post the relevant part of your LSL script (the llDialog() call and whatever code initializing the variables used in that llDialog() call), or with your operating system's configured locale; the viewer only knows about English (no UI translators/contributors), and maybe you configured a different (UTF-8 only) locale for scripts (in the "Preferences" floater, "General" tab) ?...

Also, you can use the Lua console ("CTRL ALT L" to pop it up) to test your Lua scripting; in this case, try:
Code:
s = "Dialog test\nPush the Button, Max" -- Or whatever string you use in your LSL script for llDialog()
local i, j = string.find(s, "Max")
print(i, j)
Which should print:
Code:
30
32
If you get different values, there is a character encoding issue... See Unicode and Lua.

For any further investigation by me, please also post the viewer log file (with a log obtained running your script with the "Lua" debug tag enabled, via "Advanced" -> "Consoles" -> "Debug tags"); I'm tired to spend time poking in the dark to try and solve your issue...


2024-05-27 09:24:09
Profile WWW

Joined: 2024-05-09 06:01:01
Posts: 8
Reply with quote
I am assuming it's an encoding issue and working on a function to convert the characters in the message to a list of codes.

From experimenting, it seems the problem is with the "a" and the "x", so that limits what codes I have to get.

Thanks for looking at this.


2024-05-28 04:26:53
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.