Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

8.5 and lassign
Dear Core Folks,

I'll like to put in my two bits re lassign.

In a bit, I'll insert my hand-rolled lassign; the one I use for my
work.  But now I'll list the key behaviors that I know and love:

o  The return code is the number of actual assignments made.

o  null assignments are made to excess varNames.  I might fix
this to *not* do these assignments.

o  Note the '-gatherlast' flag:  I like this *a lot*.
It puts all excess list elements at the tail end of the
list into the last varName, as a list.  It comes in very
handy.

Of course, if you don't implement these things, I'll just have to
decide when to use the core lassign and when to override it with my
own, considering realtime issues and suchlike.  But thanks for tuning
in for a bit.

- Ron A. Zajac, Dongshih Taiwan

Now:  The code:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
#
#  Distributively assign "List" element values to subsequent varNames
("args").
#    Return the number of values actually assigned.
#
proc lassign {List args} {

#  If "List" is actually the flag "-gatherlast", then
#    our actual List is the zeroth element of "args".
#
if {[set Gather [string match {-gatherlast} $List]]} {

set List [lindex $args 0]
set args [lrange $args 1 end]
}

#  We'll be returning the length of the shortest list
#    ("List" values list length vs. "args" variable names list
length).
#  This is the number of "real" assignments made.
#
set rc \
[expr \
{[set L [llength $List]] < [set V [llength $args]]
? $L
: $V}]

#  If we WISH to Gather, and we HAVE anything to gather, then...
#
if {$Gather && ($L > $V)} {
#
#  ...give our List a makeover so the following algorithm
#    will effect this gathering.
#
set List \
[concat \
[lrange $List 0 [expr $V - 2]] \
[list [lrange $List [expr $V - 1] end]]]
}

#  Loop over the varNames, assigning the List elements to each in
turn.
#  Note we assign null values to tail-end varnames for which there
are
#    no associated List elements.  These are "non-real"
assignments!
#
foreach arg $args {

upvar $arg _lassign_ThisVar

set _lassign_ThisVar [lindex $List 0]
set             List [lrange $List 1 end]
}

return $rc
}


Report this thread to moderator Post Follow-up to this message
Old Post
razajac@gmail.com
04-22-05 08:58 AM


Re: 8.5 and lassign
razajac@gmail.com wrote:
# Dear Core Folks,
#
#   I'll like to put in my two bits re lassign.
#
#   In a bit, I'll insert my hand-rolled lassign; the one I use for my
# work.  But now I'll list the key behaviors that I know and love:
#
#   o  The return code is the number of actual assignments made.
#
#   o  null assignments are made to excess varNames.  I might fix
#      this to *not* do these assignments.
#
#   o  Note the '-gatherlast' flag:  I like this *a lot*.
#      It puts all excess list elements at the tail end of the
#      list into the last varName, as a list.  It comes in very
#      handy.

My setp returns a list of remaining unused elements.

while {[llength argv]} {
set argv [setp {p v} $argv]
switch -- $p {
..
}
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?

Report this thread to moderator Post Follow-up to this message
Old Post
SM Ryan
04-22-05 08:58 AM


Re: 8.5 and lassign
razajac@gmail.com <razajac@gmail.com> wrote:
> Dear Core Folks,
>   I'll like to put in my two bits re lassign.
>   In a bit, I'll insert my hand-rolled lassign; the one I use for my
>     work.
[snipped list of differences to 8.5-core-lassign]

>   Of course, if you don't implement these things, I'll just have to
> decide when to use the core lassign and when to override it with my
> own, considering realtime issues and suchlike.  But thanks for tuning
> in for a bit.

Actually, I prefer the current 8.5-lassigns behaviour, and I
hope it will not change.

In your place I'd just rename your version of lassign to
(e.g.) mylassign, rzlassign or something else, so then you
need not choose one for each script, but can use both at will.

PS: your version also has some subtle divantages:
> proc lassign {List args} {
>     if {[set Gather [string match {-gatherlast} $List]]} {
The first argument will always be (internally) converted to
string, even if it is a (perhaps large) list. This can lead
to noticeable slowdown.


Report this thread to moderator Post Follow-up to this message
Old Post
Andreas Leitgeb
04-22-05 01:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:28 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.