Searching in "table"
[1/6] from: kpeters:otaksoft at: 4-Sep-2007 20:16
I am= struggling with this problem: Given the "table" table: [   [3 "Tim" "Peterson"
2 ]=   [ 1 "John" "Smith" 3= ]   [ 2 "Jeff" "Brown"  1 ]= ]
How can I search for the recordwhose= first value is 1 (= John Smith) and return its
index in the table ( = 2= in this case) ? The search criterion is only unique in= the
fist value of each "record". As always, thanks for any= help! Kai
[2/6] from: kpeters:otaksoft at: 4-Sep-2007 20:27
That was a horrible read - let me try this again:
I am struggling with this problem:
Given the "table"
table: [
[ 3 "Tim" "Peterson" 2 ]
[ 1 "John" "Smith" 3 ]
[ 2 "Jeff" "Brown" 1 ]
]
How can I search for the record whose first value is 1 (= John Smith)
and return its index in the table ( = 2 in this case) ?
The search criterion is only unique in the fist value of each "record".
As always, thanks for any help!
Kai
[3/6] from: moliad:g:mail at: 4-Sep-2007 23:29
you might want to build your table like so instead:
[1 "a" "ttt" 2 "b" "ggg" 3 "c" "geger"]
this way you can use the /skip refinement of some funcs like find and
select.
using a skip, you can then also select on which column of a fixed-size
recordset you want to find.
if your records are of various length, then encode your indexes like the
above, and add one extra value, as a block, and then add optional values
there.
-MAx
On 9/4/07, Kai Peters <kpeters-otaksoft.com> wrote:
[4/6] from: moliad:gmai:l at: 4-Sep-2007 23:33
examble using new nice data :-)
> table: [
> 3 "Tim" "Peterson" 2
> 1 "John" "Smith" 3
> 2 "Jeff" "Brown" 1
> ]
select/skip table 1 4
== ["John" "Smith" 3]
this also has the advantage of being very fast and saves a lot of ram. and
on big apps/data set this is an exponential save, as you are freeing the GC
of a lot of ram juggling, for those unneeded blocks.
-MAx
[5/6] from: moliad:gmai:l at: 4-Sep-2007 23:36
for ultimate speed, you might want to do this instead:
table: make hash! [
> 3 ["Tim" "Peterson" 2]
> 1 ["John" "Smith" 3 ]
> 2 ["Jeff" "Brown" 1 ]
> ]
select table 1
== ["John" "Smith" 3]
but its a question of tradeoffs, RAM speed, and how large/flexible your
dataset and algorythms are ... I guess the best thing is to try different
approaches and see what is best for you...
sometimes, just converting a table into another is a bigger hit in itself.
-MAx
On 9/4/07, Maxim Olivier-Adlhoch <moliad-gmail.com> wrote:
[6/6] from: carl::cybercraft::co::nz at: 5-Sep-2007 15:41
On Tuesday, 4-September-2007 at 20:27:12 Kai Peters wrote,
>That was a horrible read - let me try this again:
>I am struggling with this problem:
<<quoted lines omitted: 7>>
>and return its index in the table (2 in this case) ?
>The search criterion is only unique in the fist value of each "record".
This would be one way...
forall table [if table/1/1 = 1 [print index? table]]
But I agree with MAx that you could make it simplier by changing the the way the table's
organized.
-- Carl Read.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted