ID:173213
 
If i were to use findtext() to find a number in a text string such as "Omega 1" which is var/K for example using the following
var/K = "Omega 1"
if(findtext(K,list("1","2","3")))
//Now, here is my problem, How do I put the found number from the list into a variable but ignore the text (In this case "Omega" - Is there a way?, this will serously cut back on repetative code..

thanks in advance!
-Thorg
//ALWAYS USE DM TAGS!
var/k = "omega 1"
var/num = findtext(k,list("1","2","3"))
if(num)

Could work with maybe a bit of modding.
In response to Hazman
Thanks Haz,
One last thing though, how would I change iftext() so it searched for any numeric value in the text string?

-Thorg
In response to Thorg
If I understand you correctly, you could use text2num(). This proc takes a text string, removes and letters and gives back a number. For example, text2num("501.5") == 501.5 and text2num("Omega 1") == 1.

'K?
In response to Hazman
Gah, I've tried that in the following:
var/addedvalue = 0
for(var/M in addedmods)
view() << " <B>[M]</B>"
var/K = text2num(M)
var/num = findtext(M,K)
addedvalue += num

And regardless of M, be it "Extra force - 1" or "56" it only adds 1 to addedvalue, ><i'm overly confused, As I read it it makes sense, What am I missing?
<br/> Thanks in advanced..

-Thorg
Thorg wrote:
If i were to use findtext() to find a number in a text string such as "Omega 1" which is var/K for example using the following
var/K = "Omega 1"
if(findtext(K,list("1","2","3")))
//Now, here is my problem, How do I put the found number from the list into a variable but ignore the text (In this case "Omega" - Is there a way?, this will serously cut back on repetative code..

Nope, that's not your problem. Your problem is that findtext() doesn't take a list as an argument. You'd have to loop through possible values to find. Better still is to just use text2ascii() to find a digit.
var/i,j
for(i=1, i<=length(K), ++i)
var/ch = text2ascii(K, i)
if(ch >= 48 || ch <= 57)
// found where number begins; now find where it ends
for(j=i+1, j<=length(K), ++j)
ch = text2ascii(K, j)
if(ch < 48 || ch > 57) break
break
if(i <= length(K))
thenumber = text2num(copytext(K, i, j))

Lummox JR
In response to Thorg
That's because findtext() returns the position of something in something else. Eg. findtext("6","SixsIxsiX6") == 10//I think.
<font size=1>Oh, and I think you'd better know that you're trying to find a number (as in binary) in a text string (as in ASCII). It shouldn't work... but it might.</font>
In response to Hazman
Hazman wrote:
That's because findtext() returns the position of something in something else. Eg. findtext("6","SixsIxsiX6") == 10//I think.

NO. Read the reference before giving wrong advice.

Lummox JR
In response to Lummox JR
                        findtext proc
See also:
findText proc
Format:
findtext(T1,T2,Start=1,End=0)
Returns:
The position of T2 in T1; 0 if not found. //<-- NOTE RETURN
Args:
T1: The text string to search.
T2: The sub-text to search for.
Start: The text character position in T1 in which to begin the search.
End: The text character position in T1 immediately following the last character to search.
This instruction is NOT sensitive to the case of T1 or T2. The case-sensitive version is findText().

Example:
if(findtext("Hi There","there")==0)
world << "Not found!"
else
world << "Found!"

This outputs "Found!", since "there" is a part of the string "Hi There", ignoring case.


[EDIT] He does, however, have to two strings reversed. It should be findtext("SixsixSiX6","6").
In response to sapphiremagus
sapphiremagus wrote:
[EDIT] He does, however, have to two strings reversed. It should be findtext("SixsixSiX6","6").

Hence my curt reminder to read the reference before giving wrong advice.

Lummox JR
In response to Lummox JR
I did, honest!
I must have got confused with all that "T1" and "T2" stuff. Why not just "String" and "Source" or something? (Searching for string in source, by the way.)
In response to Hazman
Hazman wrote:
I did, honest!
I must have got confused with all that "T1" and "T2" stuff. Why not just "String" and "Source" or something? (Searching for string in source, by the way.)

When you think about that, those aren't very intuitive labels either. "Haystack" and "Needle" would be better.

Lummox JR
In response to Lummox JR
We should just be happy that the reference isn't full of "bar" and "foo" variables. :)
In response to Jon88
Yeah, that'd totally be fugged up beyond all recognition.
In response to Lummox JR
Yes, but you never specified what was wrong: his usage or his claim of the return value. I agree that the reference would have helped, but I also think your comment would have been more helpful if you had directly pointed out what was wrong.
In response to sapphiremagus
sapphiremagus wrote:
Yes, but you never specified what was wrong: his usage or his claim of the return value. I agree that the reference would have helped, but I also think your comment would have been more helpful if you had directly pointed out what was wrong.

The return var wasn't what was wrong; had the arguments been in the right order, he would have been correct.

Lummox JR
In response to Lummox JR
Ok, I'm sure we're just going to go around in circles about this, but here goes my last attempt. I merely intended that while you know what was wrong, he may not have. Especially if he got it wrong in the first place. It took me a few times to realize what was wrong with the line (not to say I'm an expert or anything).

All I wanted to do was ask you to be more specific in the future. Yes, he made the mistake and was doubly bad for doing so in helping someone else. However, he also used it as an example and even added a note: //I think.

My real point is that if I read your response and thought you meant the return value then others might also. You weren't especially clear and the usage LOOKS correct at a glance.
In response to sapphiremagus
sapphiremagus wrote:
My real point is that if I read your response and thought you meant the return value then others might also. You weren't especially clear and the usage LOOKS correct at a glance.

If the usage looks correct at a glance, but you're told it isn't, then the reference can clear up why. I said he should read the reference on it, which would clear that up. I didn't have to be clear on what was wrong; the reference does that for me.

In your case, you had already jumped to a conclusion that the usage looked correct, so when you looked it up you skipped over the arguments and went to the return value. Inasmuch as usage is more about arguments than the return value, but both matter, this was a mistake. An easy mistake to make and one I've made lots of times myself in the past, of course.

Then again one positive thing has come out of this: It showed the reference could be just a tad clearer on the arguments. In the near future that T1 and T2 will be changed to Haystack and Needle. Those will be a lot harder not to notice if they're out of order.

Lummox JR