In fact what Roger cleverly hinted at is otherwise
known as Query by Example (QBE). It can be shown that
any where clause (same as any complex boolean expression
with "and", "or" and nested parens) can be represented
as only two levels: or's at the top and and's at the
bottom (or other way around); which in turn can be represented
as a table where rows are joined with "or" after cells
within rows with "and". In relational case, the columns are
fields and the cells are operations and operands, with
"equal" as default (omitted) operator.
For example (2&{='Tom' or 4&{='McGuire') is
('';'';'Tom';'';''),:'';'';'';'';'McGuire'
+++---++-------+
|||Tom|| |
+++---++-------+
||| ||McGuire|
+++---++-------+
where as (2&{='Tom' and 4&{='McGuire') is
,:'';'';'Tom';'';'McGuire'
+++---++-------+
|||Tom||McGuire|
+++---++-------+
Working on a boxed table it can be expressed as
qbe=: +./@:(*./@:(=+.a:=])"1"_ 1) NB. IMPROVE ME!
DAT qbe ('';'';'Tom';'';''),:'';'';'';'';'McGuire'
1 1 1 0
DAT #~ DAT qbe ('';'';'Tom';'';''),:'';'';'';'';'McGuire'
+--+--+---+--+-------+
|0 |1 |2 |3 |McGuire|
+--+--+---+--+-------+
|5 |6 |Tom|8 |9 |
+--+--+---+--+-------+
|10|11|Tom|13|McGuire|
+--+--+---+--+-------+
DAT qbe ,:'';'';'Tom';'';'McGuire'
0 0 1 0
#~DAT qbe ,:'';'';'Tom';'';'McGuire'
1
where
]DAT=: (<'McGuire')(0 4;2 4)}(<'Tom')(1 2;2 2)}":&.>i.4 5
+--+--+---+--+-------+
|0 |1 |2 |3 |McGuire|
+--+--+---+--+-------+
|5 |6 |Tom|8 |9 |
+--+--+---+--+-------+
|10|11|Tom|13|McGuire|
+--+--+---+--+-------+
|15|16|17 |18|19 |
+--+--+---+--+-------+
--- Roger Hui <[EMAIL PROTECTED]> wrote:
> See http://www.jsoftware.com/jwiki/Essays/Inverted_Table
> If your data is appropriately organized as an inverted table
> you should be able to do
>
> (<(2 4{dat) tmemberof ,:&.>'Tom';'McGuire') #&.> dat
>
> Setting up the search target requires some care.
> You need to ensure that each opened item is a
> matrix (thus the ,:&.>) and that each opened item
> has the correct number of character columns
> (not shown above).
>
> Once you have done this, you can define a dyad
> whose left argument is the DB ("dat") and whose
> right argument are the target values, with a:
> indicating "don't care". e.g.
>
> where=: dyad define
> x #&.>~ <x tmemberof&((a: I.@:~: y)&{) y
> )
>
> dat where a:,a:,(<,:'Tom '),a:,(<,:'McGuire '),a:, ...
>
>
>
> ----- Original Message -----
> From: The World <[EMAIL PROTECTED]>
> Date: Tuesday, March 4, 2008 9:04
> Subject: [Jgeneral] best practice for argument handling
> To: General forum <[email protected]>
>
> > I want to pull all rows from a boxed table that I've created in
> > J, based
> > on specific values in certain columns. This is similar
> > to using WHERE <colvalue> = <value> to formulate SQL
> > queries. In J I
> > can do this by hand using standard J operators. Now I would like
> > to pull
> > it into a dyadic verb but I'm having trouble deciding the most
> > common
> > way to formulate the arguments. The y value is easy it will the
> > variable
> > that holds the table data (in this case I call it "dat"). But
> > now having
> > used my y argument I now need to stuff into the x argument the
> > column
> > numbers I want to base selection from and the strings that will
> > be used
> > to test for equality. Just so you get a feeling for what I'm
> > trying to
> > do the by hand formulation is as follows:
> >
> > ((2 }"1 dat) = <, 'Tom') *. (4}"1 dat) = <, 'McGuie'
> >
> > The above giving me a boolean array where the ones
> > represent the rows
> > where col 2 = Tom and Col4 =McGuire. Then I can just apply
> > the answer
> > with copy (#) to give me a new array with just those rows that
> > are
> > equal in col2 and col4.
> >
> > So is there a better way to organize this? Do you make the table
> > "dat"
> > global freeing up the y argument? I can certainly brute force
> > this but
> > I'm hoping for a more elegant solution that will be applicable
> > to future
> > problem spaces.
> >
> > Tom McGuire
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm