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
Zhiroc
Adept


Joined: 04 Feb 2005
Posts: 246

PostPosted: Fri Jan 11, 2008 5:26 pm   

Suggestion: add default value to %db() function
 
I find myself often doing:
Code:
#IF (%iskey(@x, k)) {$val = %db(@x, k)} {$val = "default"}

or something similar. It would save time (both CPU and coding) as well as being clearer (I think) to have a default value option, much like NVL() in most SQL languages:
Code:
$val = %db(@x, k, "default")
Reply with quote
Zugg
MASTER


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

PostPosted: Fri Jan 11, 2008 6:01 pm   
 
Nice idea. I'll add that to the wish list.
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Fri Jan 11, 2008 6:20 pm   
 
That's a great idea. It'll probably get put in during the Database re-write.
_________________
Asati di tempari!
Reply with quote
ReedN
Wizard


Joined: 04 Jan 2006
Posts: 1279
Location: Portland, Oregon

PostPosted: Fri Jan 11, 2008 6:22 pm   
 
I like that idea Zhiroc, I do that a lot too.

On a similar note, when doing:

$val = %db(@x, k)

I have always assumed I need to do a %iskey(@x, k) before trying to access it. Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x? That'd save me some code if I could always assume that trying to access a key that isn't there returned a known quantity.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri Jan 11, 2008 7:10 pm   
 
Could just use Lua instead

val = var[key] or "default"


;P
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Zugg
MASTER


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

PostPosted: Fri Jan 11, 2008 8:20 pm   
 
Quote:
Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x?

Yes, it should.
Reply with quote
Zhiroc
Adept


Joined: 04 Feb 2005
Posts: 246

PostPosted: Fri Jan 11, 2008 8:26 pm   
 
ReedN wrote:
I have always assumed I need to do a %iskey(@x, k) before trying to access it. Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x? That'd save me some code if I could always assume that trying to access a key that isn't there returned a known quantity.

I believe you can count on %null(%db(...)) returning true if the key doesn't exit. However, it also returns true if the key is present, but has a blank value. Sometimes you can have code that wants to tell the difference, and in that case, using %iskey() is the only way.
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