MOO-cows Mailing List Archive

[Prev][Next][Index][Thread]

Re: a programming prob.



miked@freenet.edmonton.ab.ca wrote:
> 
> I was wondering if anyone here could help me out. say I had a property
> that looked like this...
> 
> OBJECT.PROP = {"foo", {"bar", {"hello", "there"},
>                "qwerty", {"quaz", {"poi", {"moo", "cows"}}}}};
> 
> and I had a verb that would return the numbers...
>       {2, 4, 2, 2, 1}
> 
> so OBJECT.PROP[2][4][2][2][1] would be "moo".
> How would I get another verb to find "moo" using those numbers without
> always having to have the same amount of numbers?
> 
> Please post up any ideas. Thanks alot.

You mean to search that property for the string "moo"?  Heh, there's entire 3 
unit courses at any university's computer science curriculum just to answer that 
one question :-)

One way would be to recursively search for it, depth first... (Warning, I haven't 
tried compiling this verb, so it may have a gazillion horrid bugs in it just 
waiting to befound.  The general idea is there, though)

@verb object:search tnt rxd
":search(list, value)"
kids = {};
for i in [1..length(args[1])]
  if (args[1][i] == args[2])
    return {i};
  elseif (typeof(args[1][i] == LIST)
    kids = listappend(kids, i);;
  endif
endfor
if (!kids)
  return 0;
endif
for i in [1..length(kids)]
  $command_utils:suspend_if_needed();
  if (find = this:search(args[1][i], args[2])
    return {i, @find};
  endif
endfor
return 0;
.

This is the easiest tree search algorithm to write that I know of.  There are 
tons more, that may be much faster for speicific uses and searches.  Width first 
searching, which is best if what you're looking for will probably be pretty close 
to the root node (the bottom).  Heuristic searching, which is used in artificial 
intelligence programs a lot.  The list goes on.  If you want more info, there are 
tons of mightily thick books you can find on tree search algorithms.

Be careful that you don't exceed the maximum verb depth if you use it.

Phantom


References:

Home | Subject Index | Thread Index