Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Fri Dec 03, 2010 10:54 pm   

[3.32] #call %replace
 
Please see this thread to explain.

Zugg stated to use the following to optimize:

Code:
#call %replace(%ref($dirList), %i, "sw")


I tried the following which did not work:

Code:
$list = {A|B|C|D|E|F|G}
#call %replace(%ref($list), "A", "one")
#call %replace(%ref($list), "B", "two")
#call %replace(%ref($list), "C", "three")
#print $list


It printed A|B|C|D|E|F|G.

The following DOES work:

Code:
$list = {A|B|C|D|E|F|G}
$list = %replace(%ref($list), "A", "one")
$list = %replace(%ref($list), "B", "two")
$list = %replace(%ref($list), "C", "three")
#print $list
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4689
Location: Pensacola, FL, USA

PostPosted: Fri Dec 03, 2010 11:42 pm   
 
%replace doesn't change the variable string, it just shows what it would look like.

your working method of defining a variable with the %replace function is the intended method to save the results.
_________________
Discord: Shalimarwildcat
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Sat Dec 04, 2010 12:31 am   
 
shalimar: That's why he said to reference the linked post above. Zugg specifically stated you can do that:

Zugg wrote:

Btw, regarding optimizing your script, don't forget the new %ref function that allows you to modify a list "in place". Instead of doing:
Code:
$dirList = %replace($dirList, %i, "sw")

you can do this:
Code:
#call %replace(%ref($dirList), %i, "sw")

This is like using the name of a list instead of @list but works with local variables where there is no way to name it without the $ character. I think CMUD does already optimize your original code when it detects that you are assigning to the same variable that was modified, so it might not actually make things any faster, but it's better practice to be aware of when you can use "by reference" to help speed things up in the future.

Also, it would speed up your script to only access %item($ret,%i) once and put that into another local variable, then use the local variable in your #IF testing.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4689
Location: Pensacola, FL, USA

PostPosted: Sun Dec 05, 2010 8:37 pm   
 
Ahhh I see it now... but according to the Knowledge Base, I really don't see how %ref is helping in this situation.

You are not referencing one variable via another.

Even if you were %replace still doesn't perform a direct change on the variable, its cosmetic only.

No offense to Zugg, but I think there is an error of logic. How does this retain the new value without assigning it?
_________________
Discord: Shalimarwildcat
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sun Dec 05, 2010 11:02 pm   
 
Well I would assume like any other language you are passing by reference to the %replace function and not by value. I am not sure where you get cosmetic. Granted, that in this case, passing by value wouldn't matter since the list is tiny. However, it was just a test and this list could contain a thousand items for example. The fact remains that it isn't working as stated.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4689
Location: Pensacola, FL, USA

PostPosted: Mon Dec 06, 2010 9:19 am   
 
By cosmetic i mean that %replace is a temporary unsaved change.
_________________
Discord: Shalimarwildcat
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Mon Dec 06, 2010 1:53 pm   
 
Shalimar, one of the big changes late in 3.xx development was allowing certain %functions (%additem, %delitem, %addkey are among the ones specifically mentioned in the version history) to affect the variable parameter directly if they use a variable by reference (varname instead of @varname). %ref() was created specifically to allow local variables to modified in the same way. %replace() is not one of the functions specifically mentioned in the version history, but Zugg is implying that it, too, is supposed to have this functionality.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Mon Dec 06, 2010 5:30 pm   
 
Rahab is correct. Only certain functions can modify a list "in place".

The %replace function works on *string* values and not on lists. Each time you call %replace, CMUD is converting the $list into a string value, and then performing the string substitution, and then returning the new string value (which you either ignore with #CALL or assign back to $list, which causes the string value to be converted back into a string list)

Using %replace like this is slow and will not work properly on nested values. You should be using the %replaceitem to replace items within a list.

OldGuy: Sorry I made the mistake in my original post and did not read your script correctly. When I saw %replace I was thinking about %replaceitem.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net