Regular Expressions

Avantar
30 Nov 2013, 14:39Me again...stuck
If I have a word, let's say 'Hellfire' and I want to change the name to only 'fire' or 'hell' via regular expressions - how would I do it.
The best I could come up with is:
This,of course, does not work.
Any help would be appreciated.
Ps: I guess what best describes what I want to do, is to remove the first 4 letters of the word to create the object's new name.
If I have a word, let's say 'Hellfire' and I want to change the name to only 'fire' or 'hell' via regular expressions - how would I do it.
The best I could come up with is:
object.name=msg ^\w{4}(?<this.name>)$
This,of course, does not work.
Any help would be appreciated.
Ps: I guess what best describes what I want to do, is to remove the first 4 letters of the word to create the object's new name.
The Pixie
30 Nov 2013, 16:59Does it have to be with RegEx? I would use the Left or Right functions.
http://quest5.net/wiki/Category:String_Functions
http://quest5.net/wiki/Category:String_Functions

Avantar
30 Nov 2013, 18:16Thank you again Pixie!
Nope, it does not have to be regex. I didn't know there was a left$ and right$ function.
I must say, my game template wouldn't have been a 1/4 done if it wasn't for your and Pertex's scripts that you have posted.
Also just want to mention HK and Jaynabonne for their tremendous effort in helping me with the weapon equiping.
Thanks again guys/ladies! (not sure)
Nope, it does not have to be regex. I didn't know there was a left$ and right$ function.
I must say, my game template wouldn't have been a 1/4 done if it wasn't for your and Pertex's scripts that you have posted.
Also just want to mention HK and Jaynabonne for their tremendous effort in helping me with the weapon equiping.
Thanks again guys/ladies! (not sure)

Avantar
30 Nov 2013, 18:41Well, Pixie.
Since it is your script I am busy with, maybe you can help; since the idea I had does not work.
The script I am using from you is the 'Shop'.
So with the shop, you are able to buy, but not sell. I added the selling part, but if the shop sells shoes and you buy some shoes, it clones it obviously and create the name shoes1,2,3....
If I want to sell shoes I have bought from the shop, it will ask me which shoes I want to sell - it also includes the shoes in the shop; since it is visible too.
That is why I wanted to actually change the actual name of the shoes in the shop and the cloned shoes (but not shoes1) - but you are not allowed to do that outside editor mode. Come to think of it, it still wouldn't have worked anyway - it would end up trying to make duplicate names.
Would there be a way around this?
Since it is your script I am busy with, maybe you can help; since the idea I had does not work.
The script I am using from you is the 'Shop'.
So with the shop, you are able to buy, but not sell. I added the selling part, but if the shop sells shoes and you buy some shoes, it clones it obviously and create the name shoes1,2,3....
If I want to sell shoes I have bought from the shop, it will ask me which shoes I want to sell - it also includes the shoes in the shop; since it is visible too.
That is why I wanted to actually change the actual name of the shoes in the shop and the cloned shoes (but not shoes1) - but you are not allowed to do that outside editor mode. Come to think of it, it still wouldn't have worked anyway - it would end up trying to make duplicate names.
Would there be a way around this?
george
30 Nov 2013, 19:20It sounds like all of your items should have unique names, but then also an alias (that may not be unique). Then you could exclude the shop shoes (by unique name) in your script that checks for what is sellable.
When you clone items for your shop, what you could do is create the unique name by adding a number ID to the item name. Keep the last number ID in a separate game attribute, and when you use it, increase it by 1.
When you clone items for your shop, what you could do is create the unique name by adding a number ID to the item name. Keep the last number ID in a separate game attribute, and when you use it, increase it by 1.
HegemonKhan
01 Dec 2013, 01:27Pixie and George can help you much better than I, as they're skilled coders, but maybe I can help you with your "hellfire" question (if this is what you wanted):
can't you just use the "split" and "join" functions?
http://quest5.net/wiki/Split
http://quest5.net/wiki/Join
--------------
Split:
split ("male;female",";") -> (1) male (2) female
show menu ("What is your gender?", split ("male;female",";"), false) {
-> player.gender_string = result
}
P.S.
when I do:
<start type="script">
-> hellfire_string_list = split ("hell;fire",";")
-> msg (hellfire_string_list)
</start>
Returning Value: List: hell; fire;
why do I get the last semicolon after fire? (I know this is the coded value return, but the un-needed semicolon is annoying)
This doesn't occur when using "split" within the "show menu" function.
-------------
Join:
Can someone show me what the syntax would look like, as I can't figure it out from the wiki link ???
I understand (hopefully this is correct) that what "join" does is this (similiar to or same as concatenation):
(1) "hellfire" -> split -> (1) "hell", and (2) "fire"
and join is the inverse...
(1) "hell", and (2) "fire" -> join -> (1) "hellfire"
or is this not what the join does?
but I can't figure out what the syntax is for the "Join" function,
so, I'd be very appreciative if someone could show me an example of how to do the syntax for the "join" function.
I've tried:
1. join ("hell;fire",";")
<game name="blah">
-> <hellfire_string_list type="simplestringlist">hell;fire</hellfire_string_list>
</game>
2. join (game.hellfire_string_list)
3. join (game.hellfire_string_list, "hell;fire", ";")
is this below the correct syntax? (I haven't tried this yet...)
join ("game.hellfire_string_list.hell;fire",";")
~OR~
join ("game.hellfire_string_list.hell;game.hellfire_string_list.fire",";")
can't you just use the "split" and "join" functions?
http://quest5.net/wiki/Split
http://quest5.net/wiki/Join
--------------
Split:
split ("male;female",";") -> (1) male (2) female
show menu ("What is your gender?", split ("male;female",";"), false) {
-> player.gender_string = result
}
P.S.
when I do:
<start type="script">
-> hellfire_string_list = split ("hell;fire",";")
-> msg (hellfire_string_list)
</start>
Returning Value: List: hell; fire;
why do I get the last semicolon after fire? (I know this is the coded value return, but the un-needed semicolon is annoying)
This doesn't occur when using "split" within the "show menu" function.
-------------
Join:
Can someone show me what the syntax would look like, as I can't figure it out from the wiki link ???
I understand (hopefully this is correct) that what "join" does is this (similiar to or same as concatenation):
(1) "hellfire" -> split -> (1) "hell", and (2) "fire"
and join is the inverse...
(1) "hell", and (2) "fire" -> join -> (1) "hellfire"
or is this not what the join does?
but I can't figure out what the syntax is for the "Join" function,
so, I'd be very appreciative if someone could show me an example of how to do the syntax for the "join" function.
I've tried:
1. join ("hell;fire",";")
<game name="blah">
-> <hellfire_string_list type="simplestringlist">hell;fire</hellfire_string_list>
</game>
2. join (game.hellfire_string_list)
3. join (game.hellfire_string_list, "hell;fire", ";")
is this below the correct syntax? (I haven't tried this yet...)
join ("game.hellfire_string_list.hell;fire",";")
~OR~
join ("game.hellfire_string_list.hell;game.hellfire_string_list.fire",";")
Liam315
01 Dec 2013, 02:22HegemonKhan wrote:Can someone show me what the syntax would look like, as I can't figure it out from the wiki link ???
join(game.hellfire_string_list,";") would output a string "hell;fire".
The first parameter can only be a stringlist. It cannot be two or more strings, nor can it be only selective strings from a list. If you want to do either of those two things, you will have to create a new stringlist via a script before using the output of that with the join function.
It will work on the entirety of a stringlist, placing the second character (in the example a colon) in between each string. So another example join(game.hellfire_string_list," and ") would output a string "hell and fire"
HegemonKhan
01 Dec 2013, 03:15ah, thank you very much, Liam! the "separator character" is applied to the return value (the output) itself (what I didn't realize), so there's only one input (discounting what you choose for your "separator") to the "join" function: your stringlist only (and NOT the individual strings~options of your string list)
---------
so, if this is what you wanted, Avantar, you can indeed use the "split" and "join" functions:
<object name "OBJECT">
-> <inherit name="editor_object" /> // remove this line if you're using the "game" Game Object
-> <hellfire_string_list type="simplestringlist">hell;fire</hellfire_string_list>
</object>
~OR~
OBJECT.hellfire_string_list = NewStringList ()
list add (OBJECT.hellfire_string_list, "hell")
list add (OBJECT.hellfire_string_list, "fire")
~OR~
hellfire_string_list = split ("hell;fire"," and ") // = hell and fire
hellfire_string_list = split ("hell;fire"," or ") // = hell or fire
~OR~
<object name "OBJECT">
-> <inherit name="editor_object" /> // remove this line if you're using the "game" Game Object
-> <hellfire_string_list type="simplestringlist"></hellfire_string_list>
</object>
OBJECT.hellfire_string_list = split ("hell;fire"," and ") // = hell and fire
OBJECT.hellfire_string_list = split ("hell;fire"," or ") // = hell or fire
~~~~
OBJECT.hellfire_string = join (OBJECT.hellfire_string_list,"") // = hellfire
however, separating~converting "hellfire" (OBJECT.hellfire_string) into "hell" + "fire" is a bit more tricky... lol
---------
so, if this is what you wanted, Avantar, you can indeed use the "split" and "join" functions:
<object name "OBJECT">
-> <inherit name="editor_object" /> // remove this line if you're using the "game" Game Object
-> <hellfire_string_list type="simplestringlist">hell;fire</hellfire_string_list>
</object>
~OR~
OBJECT.hellfire_string_list = NewStringList ()
list add (OBJECT.hellfire_string_list, "hell")
list add (OBJECT.hellfire_string_list, "fire")
~OR~
hellfire_string_list = split ("hell;fire"," and ") // = hell and fire
hellfire_string_list = split ("hell;fire"," or ") // = hell or fire
~OR~
<object name "OBJECT">
-> <inherit name="editor_object" /> // remove this line if you're using the "game" Game Object
-> <hellfire_string_list type="simplestringlist"></hellfire_string_list>
</object>
OBJECT.hellfire_string_list = split ("hell;fire"," and ") // = hell and fire
OBJECT.hellfire_string_list = split ("hell;fire"," or ") // = hell or fire
~~~~
OBJECT.hellfire_string = join (OBJECT.hellfire_string_list,"") // = hellfire
however, separating~converting "hellfire" (OBJECT.hellfire_string) into "hell" + "fire" is a bit more tricky... lol
The Pixie
01 Dec 2013, 12:13Avantar wrote:Well, Pixie.
Since it is your script I am busy with, maybe you can help; since the idea I had does not work.
The script I am using from you is the 'Shop'.
So with the shop, you are able to buy, but not sell. I added the selling part, but if the shop sells shoes and you buy some shoes, it clones it obviously and create the name shoes1,2,3....
If I want to sell shoes I have bought from the shop, it will ask me which shoes I want to sell - it also includes the shoes in the shop; since it is visible too.
That is why I wanted to actually change the actual name of the shoes in the shop and the cloned shoes (but not shoes1) - but you are not allowed to do that outside editor mode. Come to think of it, it still wouldn't have worked anyway - it would end up trying to make duplicate names.
Would there be a way around this?
Your sell command needs to capture the text, rather than the object, so "sell #text#" rather than "sell #object#", then match the text yourself to find an object, but only against objects in the inventory (with ScopeInventory ()).
This thread might help:
viewtopic.php?f=18&t=3807

Avantar
01 Dec 2013, 17:16Thank you so much for all the replies guys!
And thank you, thank you, thank you to you Pixie - it works a treat!
And thank you, thank you, thank you to you Pixie - it works a treat!
HegemonKhan
02 Dec 2013, 00:39You don't have to use a Command, you just need to use (include) the Command's Script lines, along with the function call, to use~activate~do the "sell_function" function. Well, depending on what~how you want to do it, will require more stuff than just this above, ask me or others if you need any help with it.
you can of course add in further (or remove too) conditions, such as if the shop owner even wants to buy the item that you're trying to sell to him.
one way to do this is to use a stringlist or object list for the items that the shop owner wants (or for what he doesn't want), and just add in the scripting "checking" for this (I can help with this if you need help).
And~or, you might want to add in scripting to make some items non-sellable ("key~quest" type of items, and~or "cursed" type of items, that no one wants to buy off of from you).
<command name="sell_command">
<pattern>sell #text#</pattern>
<script>
if (GetBoolean (game.pov.parent, "shop") {
sell_function (text)
} else {
msg ("This is not a shop, you must be at a shop to sell your items.")
}
</script>
</command>
<function name="sell_function" parameters="text"><![CDATA[
equipment_x = GetObject (text)
if (equipment_x = null) {
foreach (item_x, ScopeInventory ()) {
if (item_x.alias = text) {
equipment_x = item_x
} else {
msg ("You don't possess this item, so you can't sell what you don't have!")
}
}
}
if (equipment_x <> null) {
if (game.pov.parent.cash >= equipment_x.cash) {
game.pov.parent.cash = game.pov.parent.cash - equipment_x.cash
game.pov.cash = game.pov.cash + equipment_x.cash
equipment_x.parent = game.pov.parent
} else {
msg ("The shop owner unfortunately can't afford the item that you're trying to sell to him.")
}
}
]]></function>
you can of course add in further (or remove too) conditions, such as if the shop owner even wants to buy the item that you're trying to sell to him.
one way to do this is to use a stringlist or object list for the items that the shop owner wants (or for what he doesn't want), and just add in the scripting "checking" for this (I can help with this if you need help).
And~or, you might want to add in scripting to make some items non-sellable ("key~quest" type of items, and~or "cursed" type of items, that no one wants to buy off of from you).

Avantar
03 Dec 2013, 21:08Thank you HK - that realy goes into detail (even the shopkeeper has limited money - wow)
Here is what I wanted to accomplish for buy and sell:
1) I wanted to buy anything the shop offers (more than once -so cloning the objects works great)
2) I also wanted to re-buy stuff I have sold.
3)The equipment that I sell that is not in the shop, should not be cloned again when I buy it back.
(Else I can sell a weapon and buy it back twice for dual wield
)
4) Selling an item should only recognize the sold item from my inventory.
So here is the shop library that worked for me with all your help:
Sorry it looks a bit messy (might be some debugging stuff left in there) and the selling part makes use of Pixie's Scopelib.aslx - been a bit lazy and probably made extra work for myself(since each object needs a selling script), but this is good enough for me.
I found an interesting problem: Say you have an item with an alias and a name, should you use 'buy item.alias', it will buy it once, but it won't be able to clone it and therefore when you buy it again - the object is missing if you buy it again with the alias name - like it moved to the player rather than cloning.
So it seems that cloning only works with the object name - Therefore my first FOR loop - to make sure you always use the object's name.
A sell-able item also needs a 'sell_object' script - selling my equipment will move it to another room called 'SoldItems' and selling stuff that belongs to a shop will simply remove them.
Then in my room description I have as an example:
At the top, you would list all the stuff you have in the shop and the second message will show only if it finds 1 or more items in the room 'SoldItems'
I have a few other attribute checks in my room description, but this is the general idea.
Thanks for all the help!!
Here is what I wanted to accomplish for buy and sell:
1) I wanted to buy anything the shop offers (more than once -so cloning the objects works great)
2) I also wanted to re-buy stuff I have sold.
3)The equipment that I sell that is not in the shop, should not be cloned again when I buy it back.
(Else I can sell a weapon and buy it back twice for dual wield

4) Selling an item should only recognize the sold item from my inventory.
So here is the shop library that worked for me with all your help:
<library>
<command name="buy_stuff">
<pattern>buy #text#; purchase #text#</pattern>
<script><![CDATA[
object_x = GetObject (text)
foreach (i, AllObjects()) {
if (i.alias=text){
text = i.name
object_x=GetObject (text)
}
}
if (object_x = null) {
foreach (obj, AllObjects()) {
if (obj.alias=text or obj.name=text) {
object_x = obj
}
}
}
if (object_x=null){
msg ("There is no such item!")
}
else {
if (GetBoolean(player.parent, "shop")) {
if (HasInt(object_x, "price")) {
if (player.money < object_x.price) {
msg ("You can not afford it.")
}
else if (not GetAttribute (object_x,"parent")=SoldItems and not object_x.parent = player.parent){
msg ("There is no such item here!")
}
else if (GetAttribute (object_x,"parent")=SoldItems){
msg ("You buy a " + object_x.name + " for " + object_x.price + " coins.")
object_x.parent = game.pov
player.money = player.money - object_x.price
}
else {
msg ("You buy a " + object_x.name + " for " + object_x.price + " coins.")
b = Clone(object_x)
MoveObject (b, player)
player.money = player.money - object_x.price
b.take = True
b.scenery = False
}
}
else {
msg ("That item is not for sale.")
}
}
else{
msg ("You are not in a shop.")
}
}
]]></script>
</command>
<command name="sell_stuff">
<pattern>sell #text#</pattern>
<script><![CDATA[
if (GetBoolean(player.parent, "shop")) {
ProcessScopeCommand ("sell_object", ScopeInventory(), text, "You do not have this item in your inventory.", "You can not sell this item.")
}
else {
msg ("[NotAShop]")
}
]]></script>
</command>
Sorry it looks a bit messy (might be some debugging stuff left in there) and the selling part makes use of Pixie's Scopelib.aslx - been a bit lazy and probably made extra work for myself(since each object needs a selling script), but this is good enough for me.
I found an interesting problem: Say you have an item with an alias and a name, should you use 'buy item.alias', it will buy it once, but it won't be able to clone it and therefore when you buy it again - the object is missing if you buy it again with the alias name - like it moved to the player rather than cloning.
So it seems that cloning only works with the object name - Therefore my first FOR loop - to make sure you always use the object's name.
A sell-able item also needs a 'sell_object' script - selling my equipment will move it to another room called 'SoldItems' and selling stuff that belongs to a shop will simply remove them.
Then in my room description I have as an example:
a = 0
msg ("You can buy a hat for 6 gold coins or a coat for 11.")
foreach (i, AllObjects()) {
if (GetAttribute (i,"parent")=SoldItems) {
a = a + 1
if (a>0) {
msg ("You can re-buy your sold items: " + i.name + " for " + i.price + " coins.")
}
}
}
At the top, you would list all the stuff you have in the shop and the second message will show only if it finds 1 or more items in the room 'SoldItems'
I have a few other attribute checks in my room description, but this is the general idea.
Thanks for all the help!!
The Pixie
03 Dec 2013, 22:09You should really be able to match against alias, name and alt names too.
I would set a flag on objects in the inventory. When the player wants to buy, if the item is flagged as in the inventory, clone it, otherwise give the original.
When the player is selling, if the object is flagged, you know it is just a clone, so should be destroyed. Otherwise put it in stock.
I would set a flag on objects in the inventory. When the player wants to buy, if the item is flagged as in the inventory, clone it, otherwise give the original.
When the player is selling, if the object is flagged, you know it is just a clone, so should be destroyed. Otherwise put it in stock.

Avantar
04 Dec 2013, 03:49I have tested it and it seems to work fine....
You can sell gloves and re-buy it or buy stuff in the shop and re-sell them.
I have created an exit to the room 'SoldItems' fo testing purposes to see that the gloves do move there.
You can sell gloves and re-buy it or buy stuff in the shop and re-sell them.
I have created an exit to the room 'SoldItems' fo testing purposes to see that the gloves do move there.