| 03-14-2004, 11:10 PM | #1 |
I'm trying to make a function that picks up a word with an optional offset in a string. if the offset is zero then the function will just search for the word but if the offset is a number, lets say one, then it should search for that word with the offset one. lets look at an example: string: this is a test word: test offset: 1 if the following words are present in string then they should return the value found because of the offset being one, the character x is used to represent any possible single character: test txest texst tesxt xest txst text tesx My function is able to pick up almost all of the above but it has problems with things like texst because no matter where i put the word test under it three characters will not line up. Please tell me how to modify my code to make it work correctly. Code:
function filter takes string source, string word, integer offset returns string local integer loop_source = 1 local integer loop_word = 1 local integer match = 0 loop exitwhen (loop_source > StringLength(source)) if (SubStringBJ(source, loop_source, loop_source) == SubStringBJ(word, loop_word, loop_word)) then set match = match + 1 set loop_source = loop_source + 1 set loop_word = loop_word + 1 if (match >= StringLength(word) - offset) then return "found" endif else set loop_source = loop_source + 1 set loop_word = loop_word + 1 endif if (loop_word >= StringLength(word) ) then set loop_source = ((loop_source + 1) - StringLength(word)) set loop_word = 1 set match = 0 endif endloop return "not_found" endfunction |
| 03-15-2004, 09:36 AM | #2 |
Code:
if (SubStringBJ(source, loop_source, loop_source) == SubStringBJ(word, loop_word, loop_word)) then set match = match + 1 set loop_source = loop_source + 1 set loop_word = loop_word + 1 if (match >= StringLength(word) - offset) then return "found" endif else set loop_source = loop_source + 1 set loop_word = loop_word + 1 <--- remove this I think endif When it tries to match an x in texst, it can't, and then you increase loop_word, which means the next character in word (test) will be t, instead of s. |
| 03-15-2004, 10:57 AM | #3 |
Code:
if (SubStringBJ(source, loop_source, loop_source) == SubStringBJ(word, loop_word, loop_word)) then set match = match + 1 set loop_source = loop_source + 1 set loop_word = loop_word + 1 if (match >= StringLength(word) - offset) then return "found" endif else set loop_source = loop_source + 1 set loop_word = loop_word + 1 <--- remove this I think endif ok not incrimenting loop_word when a match is not made enabled the function to pick up texst but now it cant pick up xest or txst. any more ideas? remember it should be able to pick up the following bc of the offset being one: test txest texst tesxt xest txst text tesx |
| 03-16-2004, 07:44 PM | #4 |
^_^ nyone? |
| 03-17-2004, 02:35 PM | #5 |
matching texst and txst are 2 very different things. In the first case, you shouldn't increase word_loop, but in the second, you should. This is getting more complex than writing a regular expression engine in JASS if you're not careful. Maybe you should reconsider what you are really trying to achieve here. Why do you need this function in a war3 map? |
| 03-18-2004, 11:04 PM | #6 | |
Quote:
When you can't do something you try and tell your self that since it has no reasonable application then it doesn't matter that you can't do it. The functions application shouldn't matter in this case. I've stated my objective and I am asking for help not asking for people to tell me that it's pointless. God forbid someone learns something trying a new function. AFAIK what I'm trying to do is possible, I've attempted to do it yet I'm missing something. Either it can't be done (it should be doable) or my function needs to be changed a lot to make it work. I was hoping you people would be able to point out what's wrong with my function or how to expand upon it to complete my goal but instead I get one attempt where the person didn't even TRY the outputs (me running the function and you running it are not two diff things, it either works or it doesn't) then I have this wonderful post saying well I'm really to lazy to even try solving it and since it doesn't do anything then that's OK. Maybe you have another reason for posting that's somehow indirectly related to your question of if it does something. I have considered what I am trying to achieve and why it's necessary in the WC3 map, I am asking for help with the function. People ask for help in math to get something to work and often its not the end result that useful, say a formula, its what you _do_ with it. I fail to see why you want to change the subject/avoid talking about ways to make a function do what I want instead of trying to help me solve my problem. If there is no one here who can help me then I will continue to work on it alone, shame on me for thinking other intelligent life exists on forums. |
| 03-19-2004, 10:23 AM | #7 |
Did you read jmoritz post except his last part? I imagine you're trying to do this for some anti-bad-language system or sumtin. However, as jmoritz said, sensing shiit and $hit are 2 VERY different things, so sensing them requires two different systems. Thus making this function much performance-heavyer than one would want. That's what I imagine bothers jmoritz, as coders prefer to make smart code than sequential, heavy code...and for this instance there is no way to do it smartly, instead one needs to make two types of checks, and run them in order. And if you didn't know, "Regular Expressions" are advanced searching (matching) system that has syntax to allow you to search for most anything. The syntax is something like this: "[a-zA-Z0-9_]*" That's why he was asking you to reconsider what you're trying to achieve as, if applicable to your needs, then a Regular Expression function could be better than this. But that's your sollution...to do the two different checks in a row. Good luck. Cubasis |
| 03-21-2004, 03:03 AM | #8 | ||
Quote:
I know that once the trigger is complete it will not be able to be used do to the amount of lag it would generate but I would like to know how to complete my function anyway. I have tryed adding an additional check and it did not seem to work. I will post my updated code. Code:
function filter takes string source, string word, integer offset returns string local integer loop_source = 1 local integer loop_word = 1 local integer match = 0 loop exitwhen (loop_source > StringLength(source)) if (SubStringBJ(source, loop_source, loop_source) == SubStringBJ(word, loop_word, loop_word)) then set match = match + 1 set loop_source = loop_source + 1 set loop_word = loop_word + 1 if (match >= StringLength(word) - offset) then return "found1" endif else set loop_source = loop_source + 1 set loop_word = loop_word + 1 endif if (loop_word >= StringLength(word) ) then set loop_source = ((loop_source + 2) - StringLength(word)) set loop_word = 1 set match = 0 endif endloop set loop_source = 1 set loop_word = 1 set match = 0 loop exitwhen (loop_source > StringLength(source)) if (SubStringBJ(source, loop_source, loop_source) == SubStringBJ(word, loop_word, loop_word)) then set match = match + 1 set loop_source = loop_source + 1 set loop_word = loop_word + 1 if (match >= StringLength(word) - offset) then return "found2" endif else set loop_source = loop_source + 1 set loop_word = loop_word + 1 endif if (loop_word >= StringLength(word) ) then set loop_source = ((loop_source + 2) - StringLength(word)) //set loop_word = 1 set match = 0 endif endloop return "not_found" endfunction Now please tell me how to edit my function to make it do as I wish. Quote:
|
| 03-22-2004, 08:12 PM | #9 |
yay all that and no one knows how to fix it.. |
| 03-24-2004, 02:00 AM | #10 |
:gruntsad4: ny smrt ppl out there? |
| 03-24-2004, 02:37 AM | #11 |
Stop bumping your own thread so often. I'd change the structure of your code to split it up into 2 functions, one that tries to match the word from a certain position on and one that applies the former function on all positions. And in the second case in your function you need to not increase word_loop when there is no match and you need a different condition when you are finished. |
| 03-24-2004, 09:30 AM | #12 |
What you are asking is for us to write a function that does exactly what you want, without telling us why you need it. It doesn't work that way. Either you tell us why you need it, or be content with the help that is offered at the moment. The other reason I was asking why you need it, is because there might be a different, easier solution to your problem. As for a quick fix for the function: you simply create 2 functions, 1 for replacement chars (increase word_loop), and 1 for additional chars (not increase word_loop), then call both of them in a third function. This will not match string where characters have been replaced, and added (a combination of the two, f.ex. "txsxt"). If you want that as well, you need a more serious backtracking system. |
| 03-30-2004, 04:09 AM | #13 | |||
Quote:
|
| 04-02-2004, 12:39 AM | #14 |
Ok here are the revised questions: Given a string Word and an offset O write a function to list all variations of Word including Word. My definition of offset could be different from yours so you will have to use this example to understand what I am calling offset. If Word = test and O = 0 then the function would return test If Word = test and O = 1 then the function would return test xest txst text tesx txest texst tesxt where x represents a single character (bc O=1) If Word = test and O = 2 then the function would return test xest txst text tesx txest texst tesxt where x represents a single character or two characters (bc O=2) I'm very intrested in seeing a function that can do this, right now I have only gotten it to work for specific cases and havent been able to generalize it out. There are some smart people here so it would be nice if one of them would show me how this function is done. Thanks for your time! |
| 04-03-2004, 01:10 AM | #15 | |
Quote:
I'm not bumping it that often, anyway *bump* If i bump the thread it gets looked at. Please look at my new revised question and see if you can help me. Thank you! :D |
