• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Please help me make this code better

A

ag07

Guest
I have the following code to check the number of several different substrings in a string. My method works, but I know there must be a more efficient way to code this. I have tried using for loops, but haven't got them working.

I

Create event:

s_count = 0;
count = 0;


Key Release Alt event:
1:

if (count < 3) {
count += 1;

}

2:


switch (count) { // go thru all 3 substrings, search main string for each one

case 1:

word = "cherry";

if (string_15 != -1) {

if (string_pos(word, string_15) != 0) {
scount = string_count(word, string_15);
}
}

break;

case 2:

word = "berry";

if (string_15 != -1) {

if (string_pos(word, string_15) != 0) {
scount = string_count(word, string_15);
}
}

break;

case 3:

word = "orange";

if (string_15 != -1) {

if (string_pos(word, string_15) != 0) {
scount = string_count(word, string_15);
}
}

break;

}


Is it practical to use nested loops to do this?

Please help me make it more efficient

Thank you
 

TheouAegis

Member
What exactly is this for? What is ultimately the end goal as it pertains to your game/project? What role does the string_15 play and what role do each of the fruit words play in the context of string_15.
 
C

Cerno~b

Guest
You use a lot of redundancy in your code. My first suggestion would be to put all that duplicate code in a script that takes as argument the word string and returns your scount.
But I agree with TheouAegis, it's not really clear what your string_15 means.
 
A

ag07

Guest
Thanks for the replies.
Sorry about the missing info. "string_15" is the long string that is searched by 3 substrings.

"word" is the variable that changes to the current search term, eg, first word = "apple"; then "cherry", etc.

I put this rough draft of a script together:

scr_search:

var string_15 scount word search_substring;

string_15 = argument0;
word = argument1
scount = argument2;

search_substring = "apple cherry orange";
string_15 = "apple berry cherry banana grape watermelon plum pear peach apple cherry apple"

for (i = 0; i < string_length(search_substring); i++) { // search string_15 for each word
// in search string (?)

word = i;

for (i = 0; i < string_length(string_15) i ++) {
scount = string_count(word, string_15);

}
}

return scount;


I know its not complete, but do I have the right idea at least?

thanks again
 

TheouAegis

Member
Edit: found a typo in my code

What exactly is argument1? I can make sense of argument0 and argument2, but I don't see argument1 handled anywhere. So I'll just pretend it doesn't exist.

Code:
var string_15 = argument0, search_substring = argument1, scount, substrings, first = 0, i=0, p = string_pos(" ",search_substring);
while p
{
    p -= first;
    substrings[i++] = string_copy(search_substring,first,p);
    first += p;
}
i --;
while i
    scount += string_count(substrings[i--],string_15);
return scount;
Just brainstorming. Haven't tested it yet.
 
Last edited:
C

Cerno~b

Guest
I have the faint idea that what you are trying to accomplish with substrings might be better solved with a ds_list made up from strings. Does that sound appropriate to what you are trying to achieve?
I can only second what the other guys here said: Tell us your goals and we might offer a solution that may be better than what you are trying right now to accomplish that goal.
 
A

ag07

Guest
Again, thanks for being patient, and sorry for being unclear...

I want to find how many times several substrings appear in a long string. For example:

Long string:

string_15 = "cherry berry orange grape banana cherry pineapple melon watermelon plum peach cherry apple berry";

string of three substrings: search = "apple cherry berry ";

1. search long string (string_15) for "apple", return result: 1 ("apple" appears once in long string)

2. search long string (string_15) for "cherry", return result: 3 ("cherry" appears three times in long string)

3. search long string (string_15) for "apple", return result: 2 ("berry" appears twice in long string)


I agree it would be better to use ds_lists, but I struggle to put that together into a script.

I tried running the code from Theouaegis, shown earlier in this thread, but it keeps saying compile failed, so I must not be using arguments correctly?


thanks for any more help
 
Last edited by a moderator:
C

Cerno~b

Guest
Try this:

Code:
strings = ds_list_create()
ds_list_add(strings, "cherry")
ds_list_add(strings, "berry")
ds_list_add(strings, "orange")
// add more

var counter = 0
for (var i=0; i!=ds_list_size(strings); i++)
{
    if ("cherry" == ds_list_find_value(strings, i)) counter++
}
It creates a list equivalent to your string_15 and later counts how many of a given item are in that list.
Now extending that check to the number of occurrences for more than one entry is something I would leave up to you.

Let me know if you get stuck.
 

AllCrimes

Member
Maybe I am misunderstanding, but I think you are looking for how many times a particular substring appears within a string. You already seem to use the built in function that does this automatically for you, so I am not understanding what the issue you are having is.

Code:
var long_string="Cherry Cherry Cherry Apple Berry Cherry Orange Grape";

occurences_of_cherry=string_count("Cherry", long_string)
occurences_of_grape=string_count("Grape", long_string)
 
Last edited:

TheouAegis

Member
His issue was that he was passing all three search strings as one single string as an argument in a script. And he wasn't breaking that search string up properly.
 
A

ag07

Guest
strings = ds_list_create() ds_list_add(strings, "cherry") ds_list_add(strings, "berry") ds_list_add(strings, "orange") // add more var counter = 0 for (var i=0; i!=ds_list_size(strings); i++) { if ("cherry" == ds_list_find_value(strings, i)) counter++ }
var long_string="Cherry Cherry Cherry Apple Berry Cherry Orange Grape"; occurences_of_cherry=string_count("Cherry", long_string) occurences_of_grape=string_count("Grape", long_string)


The first quote works great using a list, second quote also works if I keep it as a string...both help, thanks all, especially Cerno-b, TheouAegis, and AllCrimes
 
A

ag07

Guest
Actually it kind of works with the following code:

strings = ds_list_create(); //long list of several fruits
search_substring = "apple cherry orange";


counter = 0;
for(i = 0; i != ds_list_size(search_substring); i++) {

word = ds_list_find_value(search_substring, i);
i --;
}
for(i = 0; i != ds_list_size(strings); i++) {

{

if (word == ds_list_find_value(strings, i))

counter++;
}
}

this returns the number of times "orange" is in the main list, but I want it to do that for all 3 fruits in the search substring.
I want to have it so the first loop get interated with a keypress. eg, pressed once, search for "apple", press again, searches "cherry", third time, searches for "orange"

So is there a way to break and resume the for loop?

thanks again
 
C

Cerno~b

Guest
Try searching for the first one, then cut that substring off. When the key is pressed the next time, the second word has become the new first one and so on.
 
A

ag07

Guest
strings = ds_list_create(); //long list of several fruits
search_substring = "apple cherry orange";

counter = 0;
for(i = 0; i != ds_list_size(search_substring); i++) {

word = ds_list_find_value(search_substring, 0);

}
ds_list_delete(search_substring, 0);
for(i = 0; i != ds_list_size(strings); i++) {

{

if (word == ds_list_find_value(strings, i))

counter++;
}
}

The above code works great, Thank you Cerno-b
 
Top