|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Thu Sep 02, 2010 10:37 am
Question about by reference and by value and also concat |
This is from another thread:
Zugg wrote: |
if your @_char_rec.name variable is large, then you need to get rid of the @ and start using the newer "by reference". Every time you use the @ sign CMUD is making a *copy* of your entire string list. And it's doing this each time through the list. Also, each reference to $val in your %concat is also making a copy of the list.
Note that all of the %item calls use the name of the variable (_char_rec) *without* the @ sign. That prevents a copy from being made. Then, it uses the #ADDKEY properly to create a nested database variable within $val instead of using %concat. This should be a *lot* faster and use a lot less memory.
|
The more I look at how you changed it to allow by reference, why then would one ever need to pass by value unless you are passing to a function to perform a calculation without changing the value of the variable? Even if you do myVar = myVar + 1 it should work correctly or #print myVar. Why make a copy to just display it? Yet I can't do that but I should be able to. I'd really love if I could do myVar += 1 or myVar++ also. To be honest it's actually kind of confusing me how this by val and by reference work compared to say C#. Obviously @ means by value and having no @ means by reference, but only part of the time...? Is there a %ref function somewhere?
Coincidentally I also discovered that you can now concat using + operator.
Example:
Code: |
myVar = "a" + " short " + "string"
you can also do
myVar = %1 + " " + %2 + " " + %3
in a trigger and it works with strings |
Then the value of myVar becomes "a short string", which is actually nice for me and what I am used to in C#. It even leaves the spaces. I'm just wondering if that was intentional or some side effect from a change or perhaps you always could and I had no idea? |
|
|
|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Thu Sep 02, 2010 10:50 am |
Also please don't change that? It makes things like the following possible:
Code: |
myVar = "This is a really really really " + @otherVar +
" really really really long string" |
Value of myVar becomes "This is a really really really breaking really really really long string" since value of otherVar was "breaking". |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Sep 02, 2010 4:47 pm |
I didn't really explain it properly. What I really meant was that if you use the @ when passing the variable to certain %functions, then CMUD makes a copy. So it doesn't make any copy in your examples above. (and yes, using the + for concat is supported syntax, so don't worry about it going away...it won't).
The %concat function probably doesn't make a copy. Only functions that accept a list argument (like %additem, %item, %db, %addkey, %delitem, etc) will make a copy. In fact, if you remove the @ when using %concat, it won't work at all since it will just concat the "name" of the variable and not the value. So sorry for that confusion.
So the issue in the script I was talking about was mainly the call to %item and not the call to %concat. CMUD really does try to be smart about when to make copies and when not to make copies. I mainly just freaked out when I saw him using %concat to create a database table instead of properly using the #ADDKEY command. |
|
|
|
chamenas Wizard
Joined: 26 Mar 2008 Posts: 1547
|
Posted: Thu Sep 02, 2010 5:29 pm |
The funny (or odd thing) was I was using #ADDKEY for a similar purpose in another part of the script, but using concat in another. I'm a really odd scripter, it causes a lot of headaches :(
|
|
|
|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Thu Sep 02, 2010 7:00 pm |
Oh wow when did that happen? I don't ever recall being able to use the + operator to concatenate.
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Sep 02, 2010 8:40 pm |
A long time. Even zMUD had it.
|
|
|
|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: Fri Sep 03, 2010 3:19 am |
Well that's embarrassing. All these years and I never realized it.
|
|
|
|
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Fri Sep 03, 2010 5:58 pm |
@oldguy2 - Senile
|
|
_________________ Taz :) |
|
|
|
|
|