ID:167515
 
With a simple say verb, with ex. msg as text, how would I go extracting the sentence they entered, and shuffle the words? Also, leave out some letters, or replace them?
Thanks in advance
proc/text2list(t as text)
var/list/L=list()
for(var/I=1, I<=lentext(t), I++)
L+=copytext(t,I,I+1)
return L
proc/list2text(list/t)
var/text/L
for(var/I=1, I<=t.len, I++)
L+=t[I]
return L
proc/textlist2wordlist(list/t)
var/list/L=list()
var/word=""
for(var/I=1, I<=t.len, I++)
var/V=t[I]
if(V==" ")
L+=word
word=""
continue
word+=V
return word

proc/wordlist2textlist(list/t)
var/list/L=list()
for(var/I=1, I<=t.len, I++)
L+=t[I]
L+=" "
return L

proc/shuffle_words(list/t)
var/list/L[t.len]
for(var/I=1, I<=t.len, I++)
label1
var/V=rand(1,L.len)
if(L[V]==null)
goto label1
else
L[V]=t[I]
return L
proc/replace_letter(list/t, r as text, rw as text)
for(var/I=1, I<=t.len, I++)
if(t[I]==r) t[I]=rw
return t
In response to CIB
Augh. No. You do not need a list for this, and moreover the only thing worse than your heinous use of goto where it is totally wrong here is that you're only using one space in place of a tab. Use two spaces per tab at least, and do not use goto, because you haven't yet learned when not to.

To scramble a word, there is a much simpler method, which does not involve bogus or badly formatted code.
proc/Scramble(txt)
txt = lowertext(txt)
. = ""
while(length(txt) > 1)
var/i = rand(1, length(txt))
. += copytext(txt, i, i+1)
txt = copytext(txt, 1, i) + copytext(txt, i+1)
. += txt


Lummox JR
In response to Lummox JR
Lummox JR contributed:
proc/Scramble(txt)
> txt = lowertext(txt)
> . = ""
> while(length(txt) > 1)
> var/i = rand(1, length(txt))
> . += copytext(txt, i, i+1)
> txt = copytext(txt, 1, i) + copytext(txt, i+1)
> . += txt


I'll add a little to this, since you mentioned leaving out or substituting letters.

proc/Scramble(txt)
txt = lowertext(txt)
. = ""
while(length(txt))
var/i = rand(1, length(txt))
if(prob(85)) . += copytext(txt, i, i+1) // 85% of letters will be left unchanged
else if(prob(65)) . += ascii2text(rand(96,122)) // 65% of changed characters will be randomized
// and 35% will be left out entirely
txt = copytext(txt, 1, i) + copytext(txt, i+1)


If course, you could change all the numbers and effects to suit the flavor of your ideal scrambler. Have fun with it!
In response to PirateHead
Thanks a bunch all!
In response to Lummox JR
Err, this is sort of off-topic, but instead of opening a new post, I will just ask here.

What is . used for? The reference says if nothing is returned it returns . which is usually null. By setting it, does that mean that it returns whatever you set . to? Are there any pros and cons to using this as opposed to just setting a var then returning that var?

-Doh
In response to XxDohxX
Yes, that is what it does.
proc/MyProc()
return 1

That is the same as the following.
proc/MyProc()
. = 1

Think of the most common example where you see it used: when calling a default function action.
mob/Move()
.=..()

mob/Move returns 1 on success and 0 on failure. Many people do just ..() in mob/Move instead of .=..(), and technically that means their Move function isn't doing its full job. I have had bugs crop up before because I accidently did only ..().

As for the pros and cons, not exactly. Making a var/return_value, manipulating that instead, and then returning that at the very end will result in the exact same thing.

Doing the following is just as fine as using the return value variable.
mob/Move()
var/ret
ret = ..()
if(ret)
if(prob(10)) setup_random_battle()
return ret

However, if you ever see code snippets made by me, a function like that would have "." where you see ret there.
In response to Loduwijk
Ahh I see, thanks for explaining that some. I've always wondered what that dot was before in your programming that you've showed.

-Doh