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
shalimar
GURU


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

PostPosted: Sun Dec 11, 2011 10:45 pm   

wordwrapping - string segments
 
The game i am playing, for some reason, whats hardcoded linebreaks after x amount of characters, when creating custom items.

What would be the easiest way to break up a string like that, but only on spaces.
_________________
Discord: Shalimarwildcat
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Mon Dec 12, 2011 12:11 am   
 
Interesting question!

How about this? Assuming that it breaks at 80 or less, and the string you want to break is $mystring...
Code:

$wordlist = %replace($mystring," ","|")
$testline = ""
$lineout = ""
#WHILE (%numitems($wordlist) > 0) {
  #WHILE (%len($testline) <= 80 AND %numitems($wordlist) > 0)) {
    $lineout = $testline
    #IF ($testline = "") {
      $testline = $wordlist.1
    } {
      $testline = %concat($testline," ",$wordlist.1)
    }
  }
  #IF ($lineout = "" AND %numitems($wordlist) > 0) {
    #SAY Warning!  This line is cannot be broken at less than 80 characters!
    $lineout = $wordlist.1
  }
  #SENDRAW $lineout
  $testline = ""
  $lineout = ""
  #CALL %pop($wordlist)
}

One possible downside of this code is that it will strip out any leading or trailing spaces, if that is a problem.
Reply with quote
shalimar
GURU


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

PostPosted: Mon Dec 12, 2011 12:31 am   
 
well i want the first line indented 3 spaces (i shoulda mentioned, sorry), but other then that, it looks great.
_________________
Discord: Shalimarwildcat
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Mon Dec 12, 2011 1:59 am   
 
Actually, Vijilante wrote something like this for me a while back.

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="format" copy="yes">
    <value>#RETURN %subregex($string,"(.{1,79})(?:$| )",%concat("\1",%crlf))</value>
    <arglist>$string</arglist>
  </func>
</cmud>


Much neater, in my opinion. Then all you have to do is @format(" This line is a string that needs to be formatted correctly and includes three spaces at the beginning of the line.")
Reply with quote
shalimar
GURU


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

PostPosted: Mon Dec 12, 2011 8:48 am   
 
After tweaking, this is my final result:

Code:
$len=%if(%2, %2, 79)
$string=%replace($string, %crlf, " ")
$string=%concat("   ", %trim($string))
#RETURN %subregex($string,%concat("(.{1,", $len, "})(?:$| )"),%concat("\1",%crlf))
_________________
Discord: Shalimarwildcat
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