GetRecentLines() and REX: unexpected output

Posted by Zim on Thu 29 Jan 2015 04:12 AM — 10 posts, 33,630 views.

#0
Here is a small script to parse map text for a mud I play. In order to test it I have manually entered the value of map_written_text.

map_written_text = "A loyal McSweeney samurai is one southeast, a door east of one north, an exit southeast of two southeast, the limit of your vision is two southeast from here, a door north of two west, a bored vendor and a fat sow are two west, the limit of your vision is two west from here, doors west and east of two north, a surly youth is two north and the limit of your vision is two north from here."

re_vision = rex.new("([Tt]he limit of your vision is .+? from here(?:,|and|\.|\s)*)")
re_doors = rex.new("((?:[Aa]\s)?[Dd]oors?.*?of\s(?:(?:one|two|three|four|five)?\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|and|,|here| )+)")
re_exits = rex.new("((?:[Aa]n\s)?[Ee]xits?.*?of\s(?:(?:one|two|three|four|five)?\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|(?:and|,|here| ))+)")
re_population = rex.new("(.+?(?:is|are)(?:(?:one|two|three|four|five)?\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|and|,| )+)")

function sort_map_constructors(text, re) -- creates numeric table containing matches
	matches = {}
	re:gmatch(text, function (m, t) 
		for k, v in pairs(t) do
			table.insert(matches, v)
		end
	end)
	return matches
end

function eliminate_matches(text, t) -- eliminates matched table values from string
	for _, v in ipairs(t) do
		text = text:gsub(v, "")
	end
	return text
end

vision = sort_map_constructors(map_written_text, re_vision)
doors = sort_map_constructors(map_written_text, re_doors)
exits = sort_map_constructors(map_written_text, re_exits)

map_written_text = eliminate_matches(map_written_text, vision)
map_written_text = eliminate_matches(map_written_text, doors)
map_written_text = eliminate_matches(map_written_text, exits)

population = sort_map_constructors(map_written_text, re_population)

Note("VISION:")
table.foreach (vision, print)
Note("DOORS:")
table.foreach (doors, print)
Note("EXITS:")
table.foreach (exits, print)
Note("POPULATION:")
table.foreach (population, print)

When I run it in the immediate window it outputs:

VISION:
1 the limit of your vision is two southeast from here, 
2 the limit of your vision is two west from here, 
3 the limit of your vision is two north from here.
DOORS:
1 a door east of one north, 
2 a door north of two west, 
3 doors west and east of two north, 
EXITS:
1 an exit southeast of two southeast, 
POPULATION:
1 A loyal McSweeney samurai is one southeast, 
2 a bored vendor and a fat sow are two west, 
3 a surly youth is two north and 

Which is exactly what I want it to do. However, when I place the script in a trigger:
^.+from here\.$
change line one to:
map_written_text = GetRecentLines (1)
and run:
A loyal McSweeney samurai is one southeast, a door east of one north, an exit southeast of two southeast, the limit of your vision is two southeast from here, a door north of two west, a bored vendor and a fat sow are two west, the limit of your vision is two west from here, doors west and east of two north, a surly youth is two north and the limit of your vision is two north from here.
in the debug simulated world output I get:

VISION:
1 the limit of your vision is two southeast from here, a door north of two west, a bored vendor and a fat sow are two west, the limit of your vision is two west from here, doors west and east of two north, a surly youth is two north and the limit of your vision is two north from here.
DOORS:
EXITS:
POPULATION:
1 A loyal McSweeney samurai is 

I can't figure out why they would be outputting different things!? Help is greatly appreciated!
Amended on Fri 30 Jan 2015 12:57 AM by Zim
#1
Today I noticed several strange glitches that make me think the problem may not be with my code: Newlines are being placed randomly (and also in specific places they shouldn't be) in the world output. My other plugins are acting bizarre as well. For example: Nick Gammon's chat redirector is now truncating sentences and omitting certain words. Other problems with the world output include: color triggers coloring only the first line in the block of text that they are matched to and certain MXP hyperlinks are displaying "PROMPT>" by them. I've tried restarting mushclient and my computer and disabling and removing plugins but that did not help. Is there anything I could do besides reinstall mushclient? I can also supply details of anything that needs elaboration.
Australia Forum Administrator #2
Quote:

Which is exactly what I want it to do. However, when I place the script in a trigger:


What do you mean exactly? In "send to script" or called from a trigger? Can you post the trigger exactly?

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
#3

<triggers>
  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^.+from here\.$"
   regexp="y"
   repeat="y"
   send_to="12"
   sequence="100"
   other_text_colour="darkred"
  >
  <send>map_written_text = GetRecentLines (1)

re_vision = rex.new("([Tt]he limit of your vision is .+? from here(?:,|and|\\.|\\s)*)")
re_doors = rex.new("((?:[Aa]\\s)?[Dd]oors?.*?of\\s(?:(?:one|two|three|four|five)?\\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|and|,|here| )+)")
re_exits = rex.new("((?:[Aa]n\\s)?[Ee]xits?.*?of\\s(?:(?:one|two|three|four|five)?\\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|(?:and|,|here| ))+)")
re_population = rex.new("(.+?(?:is|are)(?:(?:one|two|three|four|five)?\\s?(?:north|northeast|east|southeast|south|southwest|west|northwest)|and|,| )+)")

function sort_map_constructors(text, re) -- creates numeric table containing matches
&#9;matches = {}
&#9;re:gmatch(text, function (m, t) 
&#9;&#9;for k, v in pairs(t) do
&#9;&#9;&#9;table.insert(matches, v)
&#9;&#9;end
&#9;end)
&#9;return matches
end

function eliminate_matches(text, t) -- eliminates matched table values from string
&#9;for _, v in ipairs(t) do
&#9;&#9;text = text:gsub(v, "")
&#9;end
&#9;return text
end

vision = sort_map_constructors(map_written_text, re_vision)
doors = sort_map_constructors(map_written_text, re_doors)
exits = sort_map_constructors(map_written_text, re_exits)

map_written_text = eliminate_matches(map_written_text, vision)
map_written_text = eliminate_matches(map_written_text, doors)
map_written_text = eliminate_matches(map_written_text, exits)

population = sort_map_constructors(map_written_text, re_population)

Note("VISION:")
table.foreach (vision, print)
Note("DOORS:")
table.foreach (doors, print)
Note("EXITS:")
table.foreach (exits, print)
Note("POPULATION:")
table.foreach (population, print)</send>
  </trigger>
</triggers>


Note: The forum codes changed every occurrence of //s to /s in my RE.

[EDIT]

Fixed the backslashes. - Nick
Amended on Fri 30 Jan 2015 11:16 PM by Nick Gammon
Australia Forum Administrator #4
Template:summary

Please provide a summary of your world configuration:

  • Either use the scripting Immediate window (Ctrl+I) to execute: Debug ("summary")

    or

  • Install the Summary plugin (see "Summary" feature) and type "summary"

Then copy the resulting information from the output window, and paste into a Forum message.

You need version 4.55 onwards of MUSHclient to do this.

Australia Forum Administrator #5

map_written_text = GetRecentLines (1)


Why are you doing this? Why not use the matching line?


map_written_text = "%0"
#6
Oh, that does make more sense.

-------------- MUSHclient summary --------------

MUSHclient version: 4.94
Compiled: Jul 28 2014.
Time now: Friday, January 30, 2015, 6:30 PM
Client running for: 0d 00h 04m 12s
World opened for: 0d 00h 04m 05s
World connected for: 0d 00h 04m 04s
Operating system: Unknown (Platform 2, Major 6, Minor 2)
Libraries: Lua 5.1.4, PCRE 8.35, PNG 1.5.14, SQLite3 3.8.4.3, Zlib 1.2.8
World name: 'DISCWORLD', ID: 4a8d6d6f86f1dcb5229c9002
-- Scripting --
Script language: Lua, enabled: yes
Scripting active: yes
Script file: C:\Program Files (x86)\MUSHclient\scripts\exampscript.lua
Lua sandbox is 127 characters, DLL loading allowed: yes
Scripting prefix: '\\'. External editor in use: NO.
Scripting for: 1.527225 seconds.
-- Triggers, aliases, timers, variables --
** Triggers: 75 in world file, triggers enabled: yes. [Triggers]
73 enabled, 75 regexp, 61773 attempts, 615 matched, 0.151156 seconds.
** Aliases: 0 in world file, aliases enabled: yes.
0 enabled, 0 regexp, 0 attempts, 0 matched, 0.000000 seconds.
** Timers: 0 in world file, timers enabled: yes.
0 enabled, 0 fired.
Timers checked every 0.1 seconds.
** Variables: 0.
-- MCCP --
MCCP active, took 0.003771 seconds to decompress
MCCP received 16494 compressed bytes, decompressed to 135191 bytes.
MCCP compression ratio was: 12.2% (lower is better)
-- Plugins (Processing order) --
ID: f99134f19ea994a0cc0888d1, 'accelerators', (Lua, 0.018 s) Enabled [Al Va Cb]
ID: dac03e2be00a4c78f4ca637d, 'Chat_Redirector', (Lua, 0.164 s) Enabled [Tr]
ID: bff25308e015986fb1a56e9e, 'Sidebar', (Lua, 1.337 s) Enabled [Tr Al Ti Va Cb]
** Plugins: 3 loaded, 3 enabled.
-- Comms --
Connect phase: 8 (Open). NAWS wanted: NO
Received: 17309 bytes (16 Kb)
Sent: 1310 bytes (1 Kb)
Received 214 packets, sent 24 packets.
Total lines received: 1164
This connection: Sent 12 lines, received 100 lines.
Telnet (IAC) received: DO: 4, DONT: 0, WILL: 5, WONT: 1, SB: 3 [Telnet]
-- MXP --
MXP active: yes, Pueblo mode: NO, Activated: On command
MXP tags received: 960
MXP entities received: 29
MXP errors: 2
-- Commands --
Commands in command history: 10
Speed walking enabled: NO. Speed walking prefix: #
Command stacking enabled: yes. Command stack character: '`'
Accelerators defined: 62 [Accelerators]
-- Miniwindows --
Window: 'bff25308e015986fb1a56e9e1', at (0,0,180,300), shown: yes
width: 180, height: 300, position: 4, hotspots: 8, fonts: 2, images: 0
Window: 'bff25308e015986fb1a56e9e2', at (0,300,180,520), shown: yes
width: 180, height: 220, position: 11, hotspots: 1, fonts: 2, images: 0
Window: 'bff25308e015986fb1a56e9e3', at (0,520,180,586), shown: yes
width: 180, height: 66, position: 10, hotspots: 12, fonts: 1, images: 0
Window: 'bff25308e015986fb1a56e9e:info', at (0,0,0,0), shown: NO
width: 1, height: 1, position: 1, hotspots: 0, fonts: 2, images: 0
** Miniwindows: 4 loaded, 3 shown.
-- Output window --
Output pixels: width 839, height: 586, font width: 8, font height: 15
can show 104 characters, wrapping at column 80, height 39 lines.
Output buffer: 1190 of 5000 lines.
-- Miscellaneous --
Logging: NO, tracing: NO
** SQLite3 databases: 0
Sound buffers in use: 0
Amended on Fri 30 Jan 2015 11:33 PM by Zim
#7
Also, all of the odd glitches I described from yesterday seems to have sorted themselves out. Could the problem have something to do with the whitespace RE "//s"?

EDIT: Just tried switching all the occurances of "//s" with " ". Fixed it!!! I'm still not sure why "//s" makes the script act unexpectedly though.
Amended on Sat 31 Jan 2015 12:01 AM by Zim
Australia Forum Administrator #8
For a start, whitespace is: \s not /s
#9
Nick Gammon said:

For a start, whitespace is: \s not /s

...well I feel like a fool. Sorry for wasting your time, Nick. I really do appreciate your help!