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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
l33t3rth4nur1111
Newbie


Joined: 29 May 2005
Posts: 6

PostPosted: Mon Jul 18, 2005 3:50 am   

Simple function problem
 
At least, it seems like one of those disgustingly simple problems.

I wrote a simple recursive function intended to take a string and return the string with an underscore before each character. For example,
Code:
#EVA fnUnderString("123 456")

should display
Code:
_1_2_3_ _4_5_6

in the console. Here's my function:
Code:
#FU fnUnderString {%if( %len( %1) = 0, "", %concat( "_", %left( %1, 1), @fnUnderString(%right(%1,1))))}

The idea is to take the string and return the underscore, the first character, then the recursive call on the rest of the string.

For some reason, it works fine with most strings, but spaces are lost and some special characters (including the exclamation point) break it entirely. The above example produces
Code:
_1_2_3_4_5_6

in the console.

It looks like a problem of evaluation--maybe the script sees " 456" without the leading space and ignores it. Can someone help me out?
Reply with quote
Aarlot
Adept


Joined: 30 Dec 2003
Posts: 226

PostPosted: Mon Jul 18, 2005 4:59 am   
 
Well, don't know what to do about the spaces, but adding a couple %literal's to the function seems to make special characters work:

#FU fnUnderString {%if( %len( %1) = 0, "", %concat( "_", %left( %literal( %1), 1), @fnUnderString(%right(%literal(%1),1))))}


EDIT: though I just tested the original, and it didn't seem to have any problems with special chars anyway :p
_________________
Everyone is entitled to their beliefs - until they die. Then only the truth matters.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Mon Jul 18, 2005 9:57 pm   
 
The use of quotes would do it just fine. In fact I have said it many times, proper delimeter usage is required. In the case of what Aarlot said the %literal() make a delimeter.

#FU fnUnderString {%if( %len("%1") = 0, "", %concat( "_", %left( "%1", 1), @fnUnderString(%right("%1",1))))}
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
l33t3rth4nur1111
Newbie


Joined: 29 May 2005
Posts: 6

PostPosted: Wed Jul 20, 2005 3:25 am   
 
Thanks to you both; I'll give it a try and report the results.
Reply with quote
l33t3rth4nur1111
Newbie


Joined: 29 May 2005
Posts: 6

PostPosted: Wed Jul 20, 2005 3:29 am   
 
Hmm, I tried both and I still got "_1_2_3_4_5_6" instead of the desired "_1_2_3_ _4_5_6".

Would any of my zMUD preferences affect the execution of these functions? (Other than changing special characters from the default, which I have not.)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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