On Thursday, November 7, 2002, at 09:27 PM, Gabriel Ricard wrote:

I guess I'm just spoiled by the associative arrays in PHP.
Tcl arrays are associative.  Tcl has pretty much 3 data structures:

       - scalars
       - lists
       - arrays (which are associative)

The trick is that a list is really just a scalar with whitespace between
the elements, unless the element is enclosed by braces, so there's really
only two.

Here's an example of Tcl array usage, outside of the DB context:

       set likes(Bob.color) "red"
       set likes(Earl.color) "blue"

Now you can do things like:

       puts $likes(Bob.color)

You can also do this:

       set person "Bob"
       puts $likes($person.color)

And that's how a lot of the DB stuff I do gets done.  I have this routine
I use that sucks in a DB query into an associative array and gives you
back a list of the primary keys in the array (assuming the primary key is
not a composite key), and then you can do this:

       foreach rowkey $rows {
               ns_puts "Person: $rowkey; Favorite color:
$fetchResult(key.$rowkey.column.color)"
       }

The only problem with it is that it's pretty painfully slow for large
datasets, but the logic is simple enough that you can probably figure out
how it works just from this description.

Reply via email to