Message Board


Message Board > Fenix / Bennu / Gemix / DIV > Find function in fenix

August 19, 2008, 01:21
Geuben
Whiskered
11 posts
Hi all, Im after some help with the find function in fenix.

The situation:

I have a string of 7 characters which can be any combination of 1s and 0s ie 1010011. I need to be able to determine the position in the string of the 1st 1 that appears, the 2nd 1, the 3rd etc etc starting from the left.

I know that
Code:
find(string,"1") 

will return the position of the first "1" in the string and that works fine but I can't fathom a way to use this to find the 2nd or 3rd... appearance of a "1". From the Fenix Docs wiki its possibile to specify the starting position in the string. So my idea was to call the find function once and use the result as the starting position for the next call (in the case for finding the 2nd appearance).

So can anyone figure out a nicer way of doing these repetative calls rather than writing out

Code:
find(string,"1",find(string,"1")) //for 2nd
 find(string,"1",find(string,"1",find(string,"1"))) // for 3rd etc etc


Thanks in advance for any help.

Geuben
____________
#
August 19, 2008, 06:55
SplinterGU
Whiskered
16 posts
Code Bennu/Fenix Compatible

Code:
PROGRAM FindTest;

#ifdef COMPILER_VERSION
    #ifdef __VERSION__
        import "mod_say";
    #endif
    import "mod_mem";
    import "mod_string";
#endif


PROCESS main()
private
    i = 0;
    string s = "0101101";
BEGIN
    loop
        i = find(s,"1",i);
        if (i == -1) break; end
        say (i);
        i++;
    end
end


[Edited on August 19, 2008 by SplinterGU]
____________
#
August 19, 2008, 13:24
Geuben
Whiskered
11 posts
Thanks for the help, I think I may not have been clear on what I was after.

Ideally I need a function that i can pass the string and an integer too (the integer being the appearence im after i.e 1=1st, 2=2nd etc) and have it return the position of the required appearence.

Perhaps something along these lines

Code:

PROCESS Finder(string,app) 
PRIVATE
i=0;
j=0;
BEGIN
  for(i=0;i<>app;i+=1)
    j = find(string,"1",j);
  end
  RETURN(j);
END


Would that work?
____________
#
August 19, 2008, 14:15
SplinterGU
Whiskered
16 posts
here:

Code:
#ifdef COMPILER_VERSION
#ifdef __VERSION__
import "mod_say";
#endif
import "mod_mem";
import "mod_string";
#endif

function finder(string str, string key, int *context)
begin
    *context = find(str,key,*context);
    if (*context == -1)
        return -1;
    end

    return ((*context)++);
end


process main()
private
    c = 0; // Must be initialized to zero
    r;
begin
    loop
        if ((r=finder("0101101", "1", &c)) == -1)
            break;
        end
        say (r);
    end
end


[Edited on August 19, 2008 by SplinterGU]
____________
#
August 19, 2008, 14:37
Geuben
Whiskered
11 posts
Hummm.....as far as I can tell that code would print a list to the debug console consisting of:

1
3
4
6

I'm still not sure if that would be useale for what I need. Your code gives out a list of all the poisitions of 1s in the string, but im after just the position of a specific 1. I'll try and explain a little better.

The code is to handle a menu in game, there are 7 possible actions that can be on the menu. Each bit in the string is a flag to say wether the corresponding action is avaliable. The menu is dynamic so that unavaliable options are not shown, so the menu changes size everytime.

e.g. the actions are Fire,Supply,Load,Drop,Dive,Capture,Wait

if for instance at this particular time you can fire,drop and wait then the string would be 1001001 and the menu would look like so:

Fire
Drop
Wait

the aim of the finder process would be to determine which action is in which position on the menu. so to select the first option on the list I would need to find the first appearence of a 1 in the string which in this case the action is Fire and the finder would return 0 to indicate that the first option in the list is in position 0, if i wanted to select the 2nd option on the list (in this case drop) then the finder would have to return 3 which would allow me to determine that drop was selected.

does that explain it any better? probably not.
____________
#
August 19, 2008, 17:05
SplinterGU
Whiskered
16 posts
if you need ask for a determinate position, you don't must use find... only use "string[pos]"... I only ask your initial question... maybe you initial question was wrong...

if you want 1st, or 2nd, or ... available (set to 1), you can do a for until get correct value...

Code:
process main()
private
    c = 0; // Must be initialized to zero    
    r;
    i;
    item_to_get = 2; // this must be param of a function
    item_mask = "0101101";
begin
    for (i = 0; i < item_to_getloop; i++)
        if ((r=finder(item_mask, "1", &c)) == -1)
            break;
        end        
    end
    
    if (r == -1)
        say ("Item not found");
    else
        say ("Item in "+r+" position");
    end
   
end


if you ask other thing, sorry, I don't understand...
____________
#
August 19, 2008, 17:44
SplinterGU
Whiskered
16 posts
Other solution are use of bitmask and bitwise operator... this let you use mask of 1-32 items...
You must count from right to left using binary numbers. So in the case of 010110100b the 0th position is 0, the 1st is 0 the 2nd is 1, etc.
look this...

Code:
#ifdef COMPILER_VERSION
#ifdef __VERSION__
import "mod_say";
#endif
import "mod_mem";
import "mod_string";
#endif
 
function int finder(int mask, int item)
private
    i;
    c;
begin
    if (item > 31 || item < 0) return -1; end
 
    for (i = 0; i < 32; i++)
        if (mask & (1 << i))
            c++;
            if (c == item)
                return i;
            end
        end
    end
 
    return -1;
end
 
 
process main()
private
    c = 0; // Must be initialized to zero
    r;
begin
    if ((r=finder(010110100b, 2)) == -1)
        say ("don't item available");
    else
        say ("item found in "+r+" position");
    end
end


the function return -1 if item don't found...

[Edited on August 19, 2008 by SplinterGU]
____________
#
August 19, 2008, 17:49
SplinterGU
Whiskered
16 posts
well, mod_mem and c in main don't necessary...
____________
#

Message Board > Fenix / Bennu / Gemix / DIV > Find function in fenix

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!