Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
I swear, I knew this had been discussed before, I just was looking for pointers. I found the right set of things to ask Google, and came up with these http://www.pinds.com/acs-tips/tcl-data-structures Lars doesn't believe Tcl is good for this stuff http://mini.net/tcl/2995 The Tcl'ers Wiki on the topic: http://mini.net/tcl/1481 lindex in 8.4 can manipulate sublists of a list (Thanks, Jeff!) I'd still love to read more on this topic. I forgot to mention that, although I've been working in Tcl since 7.4, I am lacking in how Tcl has changed in 8.4. I'll be Googling for that, but I'd be happy for pointers on that, too. Thanks, Pete.
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
Humm, I never really found the short list of data types a hinderance for Tcl. I have always been able to manage with what is there, although I did write the record package (tcllib) to overcome one obstacle I had, but I did that more as an excercise really. If you really are stuck, and want to find some other stuff, then I know some people have loaded XOTcl into AOLserver, you may want to look into that. It gives you an OO facility. You may also want to look at TclX, which has other data types available (most notably, keyed lists). I really use lists heavily, and with Tcl8.4, list manipulation has improved quite a bit, so that's good news for me. I also use an array of lists quite a bit... I am just curious, can you give an example of something that is giving you a hard time? --brett On Fri, 2003-01-17 at 14:30, Peter M. Jansson wrote: (...that work in AOLserver.) So, I've been writing Tcl for AOLserver since it only ran Tcl 7.4, and you only had 3 choices for storing data in Tcl -- scalars, lists and arrays. Things like a list of arrays weren't possible; if you wanted to have such a thing, you had to fake it. I admit, I've gotten used to being able to do such things in Perl, where an array of hashes is an ordinary thing (or at least an array of hash references), and I have to say that I find it convenient. How do Tcl programmers normally do such things? Do they avoid messes like these, handle them in other languages, and provide Tcl commands to manipulate them? Do they use one or more Tcl add-ons? (If so, which ones work under AOLserver?) Or am I approaching the problem from the wrong perspective? What makes this an AOLserver question, rather than just a Tcl question, is that I want to make sure whatever I do would be supported within AOLserver. But other than that, it's not really an AOLserver question. Thanks, Pete. -- Brett Schwarz brett_schwarz AT yahoo.com
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
So, I've been writing Tcl for AOLserver since it only ran Tcl 7.4, and you only had 3 choices for storing data in Tcl -- scalars, lists and arrays. Things like a list of arrays weren't possible; if you wanted to have such a thing, you had to fake it. I admit, I've gotten used to being able to do such things in Perl, where an array of hashes is an ordinary thing (or at least an array of hash references), and I have to say that I find it convenient. How do Tcl programmers normally do such things? Do they As you found at: http://wiki.tcl.tk/1481 using lists and lists of lists is very efficient with 8.4. All the info is passed around by reference at the C level. For something like: set a [list \ [list a b c] \ [list d e f] \ ] set b [lindex $a 1] $b just points to the same [list d e f] object that is part of $a. All this manipulation is very fast. Some prefer to abstract the use of lindex and lset by extra procs that are more meaningful (like 'access $key'), but that's adds very little overhead. You can go beyond the core to use extensions like XOTcl to create whatever you want in terms of classes and OO objects. Jeff Hobbs The Tcl Guy Senior Developer http://www.ActiveState.com/ Tcl Support and Productivity Solutions
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
On 2003.01.17, Peter M. Jansson [EMAIL PROTECTED] wrote: (...that work in AOLserver.) [...] Things like a list of arrays weren't possible; if you wanted to have such a thing, you had to fake it. [...] How do Tcl programmers normally do such things? Generally, when I find myself needing things like, say, a list of arrays, in Tcl, they tend to be design smells. It's the I can write Perl in any language! syndrome. However, Tcl is really powerful and faking it isn't too bad. If I need a list of arrays, I'll just create a list of array references. I'll just know that if I want access to the array, I'll have to dereference to get there. set a(1) foo set b(1) bar set array_list [list a b] ns_adp_puts [set [set [lindex $array_list 0]]] :-) -- Dossy -- Dossy Shiobara mail: [EMAIL PROTECTED] Panoptic Computer Network web: http://www.panoptic.com/ He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on. (p. 70)
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
I got this... On Fri, 17 Jan 2003, Dossy wrote: Generally, when I find myself needing things like, say, a list of arrays, in Tcl, they tend to be design smells. It's the I can write Perl in any language! syndrome. ...and this: On Fri, 17 Jan 2003, Brett Schwarz wrote: I am just curious, can you give an example of something that is giving you a hard time? OK. When I'm building a substantial app, there's a lot of data floating around. When I'm pulling data out of the database, I often will have something where I have a brand that has customers that have orders that have line items, and I want to store those. I think managing complexity by abstracting the data is good programming practice. When I've done this in the 7.x-era Tcl, I've done it by using an array with specially-crafted indices, and using wrapper functions so I can do things like set orders [customers orders $customer_id] and get a list of order IDs. But I find my array implementations to be kludgy (more a defect of the class of solutions, rather than the particular implementation), so I'm always looking for something better. Also, I want to do a lot of the persistence work only once, so I want routines that pull records from the database based on criteria I specify, and carry them in a cache, and then notice when I modify the records so I can then do a cache commit to do all the updates. And I want to write those routines to be generic, so I don't have to rewrite them again for each database table. (I really did this for one project, and it mostly worked, but it was fragile.) Recently, I've gotten to do some work in WebObjects, which has a lot of this done, but in Java, and I still find value in the Tcl/AOLserver approach, so I'm looking to apply some of these ideas in Tcl, but the data structures as I knew them felt too constraining. I think the 8.4 list implementation is a step in the right direction; I don't know if it's really what I'm looking for, but I think it will be cleaner (and probably faster, given the implementation differences between arrays and lists) than the array implementation I've done. Pete.
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
On Fri, Jan 17, 2003 at 10:19:27PM -0500, Peter M. Jansson wrote: I got this... On Fri, 17 Jan 2003, Dossy wrote: Generally, when I find myself needing things like, say, a list of arrays, in Tcl, they tend to be design smells. It's the I can write Perl in any language! syndrome. ...and this: On Fri, 17 Jan 2003, Brett Schwarz wrote: I am just curious, can you give an example of something that is giving you a hard time? OK. When I'm building a substantial app, there's a lot of data floating around. When I'm pulling data out of the database, I often will have something where I have a brand that has customers that have orders that have line items, and I want to store those. I think managing complexity I've often wished -- Andrew Piskorski [EMAIL PROTECTED] http://www.piskorski.com
Re: [AOLSERVER] OTish: Data abstraction facilities in Tcl
Right or wrong I normally leave that kind of work up to the database rather than caching it in memory. I wonder how it would work to write a sql-based database in memory for cache handling. Something like: ns_db cache $postgres select * from postgresql to $ram ns_db select $ram select * from ram_cache_table where x=1 ns_db dml $ram update ram_cache_table set order='sent' ns_db flush_cache $ram $db or something. Anyhow, here's the caching scheme I came up with tonight. It uses parallel lists for columns so that you can use lsearch to locate a specific row. # procedure proc vt_cache_query {db sql args} { set selection [ns_db select $db $sql] set varprefix {dbcache} if {[llength $args] 0} { set varprefix [lindex $args 0] } uplevel set ${varprefix}_field_names \[list \] set first_run 0 while {[ns_db getrow $db $selection]} { set col_counter_i 0 set col_limit [ns_set size $selection] while {$col_counter_i $col_limit} { uplevel lappend ${varprefix}_[ns_set key $selection $col_counter_i] \{[ns_set value $selection $col_counter_i]\} if {$first_run == 0} { uplevel lappend \${varprefix}_field_names\ \{[ns_set key $selection $col_counter_i]\} } incr col_counter_i } incr first_run } } # sample code set db [ns_db gethandle] vt_cache_query $db select * from users users set current_row [lsearch $users_user_id 3] foreach item ${users_field_names} { ns_write $item = [lindex [set users_${item}] $current_row ]br } ns_write [lindex $users_first_names $current_row] [lindex $users_last_name $current_row] On Friday 17 January 2003 09:19 pm, Peter M. Jansson wrote: I got this... On Fri, 17 Jan 2003, Dossy wrote: Generally, when I find myself needing things like, say, a list of arrays, in Tcl, they tend to be design smells. It's the I can write Perl in any language! syndrome. and this: On Fri, 17 Jan 2003, Brett Schwarz wrote: I am just curious, can you give an example of something that is giving you a hard time? OK. When I'm building a substantial app, there's a lot of data floating around. When I'm pulling data out of the database, I often will have something where I have a brand that has customers that have orders that have line items, and I want to store those. I think managing complexity by abstracting the data is good programming practice. When I've done this in the 7.x-era Tcl, I've done it by using an array with specially-crafted indices, and using wrapper functions so I can do things like set orders [customers orders $customer_id] and get a list of order IDs. But I find my array implementations to be kludgy (more a defect of the class of solutions, rather than the particular implementation), so I'm always looking for something better. Also, I want to do a lot of the persistence work only once, so I want routines that pull records from the database based on criteria I specify, and carry them in a cache, and then notice when I modify the records so I can then do a cache commit to do all the updates. And I want to write those routines to be generic, so I don't have to rewrite them again for each database table. (I really did this for one project, and it mostly worked, but it was fragile.) Recently, I've gotten to do some work in WebObjects, which has a lot of this done, but in Java, and I still find value in the Tcl/AOLserver approach, so I'm looking to apply some of these ideas in Tcl, but the data structures as I knew them felt too constraining. I think the 8.4 list implementation is a step in the right direction; I don't know if it's really what I'm looking for, but I think it will be cleaner (and probably faster, given the implementation differences between arrays and lists) than the array implementation I've done. Pete.