Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Save arrays between sessions

Save arrays between sessions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Fletchling   Australia  (54 posts)  Bio
Date Mon 07 Aug 2006 01:00 PM (UTC)
Message
I have a few arrays I'd like to save between the infrequent session close/open events. If it was a table I'd be happy with the convert and save/load via a string. Do arrays work the same?

I've created the variable with the following;

<aliases>
<alias
match="savespells"
enabled="y"
send_to="12"
sequence="100"
>
<send>SetVariable ("spellssu5", ArrayExport ("su4", "|"))</send>
</alias>
</aliases>

where;
spellssu5 is the target variable
su4 is the array being exported

Help appreciated with re-creating the array on loading, thanks
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #1 on Mon 07 Aug 2006 07:30 PM (UTC)
Message
Here are a serialization and unserialization function I created for my own use. It will store a variable as basic XML.

Examples of use (Lua):

this_is_an_array = {}

world.SetVariable('VarName',serialize(this_is_an_array))
this_is_an_array = unserialize(world.GetVariable()) or {}


--SERIALIZATION FUNCTIONS

function serialize(var,compression)
	if(type(var) == 'table') then
		local output = ''
		for index,value in pairs(var) do
			if(not (value == var)) then
				output = output .. '<i i="' .. index .. '">' .. serialize(value) .. '</i>'
			end
		end
		
		if(compression) then
			return '<ct>' .. utils.base64encode(utils.compress(output,9)) .. '</ct>'
		else
			return '<t>' .. output .. '</t>'
		end
	elseif(type(var) == 'number') then
		return '<n>' .. var .. '</n>'
	elseif(type(var) == 'string') then
		return '<s>' .. var .. '</s>'
	elseif(type(var) == 'boolean') then
		return '<b>' .. (var and 't' or 'f') .. '</b>'
	end
	
	return ''
end
function unserialize(var)
	if(var == nil) then
		return nil
	end
	
	local root = utils.xmlread(var)	
	
	if(root) then
		return unserialize_xml(root.nodes[1])
	else
		return nil
	end
end
function unserialize_xml(node)
	if(node.name == 'n') then return tonumber(node.content) end
	if(node.name == 's') then return tostring(node.content) end
	if(node.name == 'b') then return (tostring(node.content) == 't') end
	
	if(node.name == 'ct') then
		node.name = 't'
		local root = utils.xmlread(utils.decompress(utils.base64decode(node.content)))
		
		if(root) then
			node.nodes = root.nodes
		else
			return {}
		end
	end
	
	if(node.name == 't') then
		local t = {}
	
		if(node.nodes) then
			for _,child in ipairs(node.nodes) do
				if(child.name == 'i') then
					t[child.attributes['i']] = unserialize_xml(child.nodes[1])
				end
			end
		end
		
		return t
	end
	
	return nil
end


I would recommend putting the functions in a seperate script file which you can include through the world scripting interface. Then your triggers can call these functions as they execute their own code. Hope that helps. -Tsunami
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 08 Aug 2006 02:55 AM (UTC)
Message
Well, ArrayImport effectively turns a string into an array, provided you create the empty array in advance.

http://www.gammon.com.au/scripts/doc.php?function=ArrayImport

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fletchling   Australia  (54 posts)  Bio
Date Reply #3 on Tue 08 Aug 2006 12:09 PM (UTC)
Message
Ilove this mush community.

I tried this and it seems to work nicely;

<aliases>
<alias
match="loadspells"
enabled="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>ArrayDelete "su5"
ArrayCreate "su5"
ArrayImport ("su5", "@spellssu5", "|")</send>
</alias>
</aliases>

I've tried to keep all my mush stuff simple so the captive crash test dummy can understand it. Thanks for the help guys.

Mr N Gammon for Aussie of the Year, and thanks Tsunami heaps.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,899 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.