Removing the values from a list from another list 
Author Message
 Removing the values from a list from another list

Hi everyone,

This is my first post to this list.  I have been using TCL for a
while now, so I hope I might be of some use to this forum
eventually :-)

I am stuck on a (smallish, I hope) problem.

I have a list from a SQL query.  From this list (L1), I have
filtered out all elements matching a particular criteria (L2).
I now want to get the rest.

Does anyone know of a way of doing this easily?  Obviously,
organisation of elements within the list are exactly the same.
I can think of doing it by stepping through L1 and appending
every item not matching L2 to a second list, but this seems a
little cumbersome.  Does anyone know of a $L1 MINUS $L2 command,
or the like?

Or, should I just go back from my original filter, and step
through L1, appending to L2 if it matches my criteria, or L3 if
it doesn't.

I know this could be resolved also by doing two SQL queries, but
I don't really want to do that as it is quite a large one.

Any help would be appreciated.

Thanks

Di

-----------------------------------------------------------

Got questions?  Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.*-*-*.com/



Mon, 20 Jan 2003 03:00:00 GMT  
 Removing the values from a list from another list

Quote:

> Does anyone know of a way of doing this easily?  Obviously,
> organisation of elements within the list are exactly the same.
> I can think of doing it by stepping through L1 and appending
> every item not matching L2 to a second list, but this seems a
> little cumbersome.  Does anyone know of a $L1 MINUS $L2 command,
> or the like?

Well, I have one in tkcon that is like so:

(tkcon) 49 % lremove {a b c d} {b c}
a d
(tkcon) 50 % dump lremove
proc lremove args {
    array set opts {-all 0 pattern -exact}
    while {[string match -* [lindex $args 0]]} {
        switch -glob -- [lindex $args 0] {
            -a* { set opts(-all) 1 }
            -g* { set opts(pattern) -glob }
            -r* { set opts(pattern) -regexp }
            --  { set args [lreplace $args 0 0]; break }
            default {return -code error "unknown option \"[lindex $args 0]\""}
        }
        set args [lreplace $args 0 0]
    }
    set l [lindex $args 0]
    foreach i [join [lreplace $args 0 0]] {
        if {[set ix [lsearch $opts(pattern) $l $i]] == -1} continue
        set l [lreplace $l $ix $ix]
        if {$opts(-all)} {
            while {[set ix [lsearch $opts(pattern) $l $i]] != -1} {
                set l [lreplace $l $ix $ix]
            }
        }
    }
    return $l

Quote:
}

The proposed extended list command has these set ops in it, which
will be useful.

--
   Jeffrey Hobbs                     The Tcl Guy
   hobbs at ajubasolutions.com       Ajuba Solutions (ne Scriptics)



Mon, 20 Jan 2003 03:00:00 GMT  
 Removing the values from a list from another list


Quote:

>> Does anyone know of a way of doing this easily?  Obviously,
>> organisation of elements within the list are exactly the same.
>> I can think of doing it by stepping through L1 and appending
>> every item not matching L2 to a second list, but this seems a
>> little cumbersome.  Does anyone know of a $L1 MINUS $L2 command,
>> or the like?

>Well, I have one in tkcon that is like so:

>(tkcon) 49 % lremove {a b c d} {b c}
>a d
>(tkcon) 50 % dump lremove
>proc lremove args {

                        .
                        .
                        .
1.  If L1 and L2 truly come from SQL, there's a good
    chance someone can come up with a single SQL com-
    mand that gives the result you want.  In some
    circumstances, that will be much faster.
2.  Jeffrey's lremove sensibly preserves order.  If
    you're willing to relax this requirement, there
    are proc definitions (acting on lists-considered-
    as-sets, essentially) that can be much faster for
    very large input lists.
--


Business:  http://www.Phaseit.net
Personal:  http://starbase.neosoft.com/~claird/home.html



Mon, 20 Jan 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. remove a list from a list

2. list.pop([i]) and list.remove(x) w/ for loops

3. remove a list from a list

4. list permutations and weired list.remove funtion

5. Turning a list value in a parameter into a list for eval without quasiquote/unquote

6. Intersection of multiple lists/list of lists

7. Multiple-value-list with no values.

8. DXOracle / PyADO - convert lists of tuples to list of lists

9. Please remove this account from the mail list

10. Removing doits from change list???

11. Remove from list

12. Remove from list.

 

 
Powered by phpBB® Forum Software