Extended [array set] functionality
Open219315ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 17 years ago
Descriptionplain2000-10-26 05:10:36
OriginalBugID: 3677 RFE
Version: 8.3b1
SubmitDate: '1999-11-23'
LastModified: '2000-10-25'
Severity: LOW
Status: UnAssn
Submitter: techsupp
OS: Windows 98
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'
Name:
Peter Spjuth
CVS:
23 nov 99
DesiredBehavior:
On several occasions I have found myself using constructs like:
foreach elem $list {
set arrayName($elem) $value
}
Each time I've done that I have wished for an extended "array set"
with the following syntax:
array set arrayName list ?value?
Where, if value is set, it is equivalent to the above.
A typical use for me is when implementing "sets" using arrays,
and wanting to add every element in a list to the set.
Patch:
Index: tclVar.c
===================================================================
RCS file: /cvsroot/tcl/generic/tclVar.c,v
retrieving revision 1.14
diff -c -r1.14 tclVar.c
*** tclVar.c1999/10/05 22:45:401.14
--- tclVar.c1999/11/23 18:03:43
***************
*** 3102,3112 ****
break;
}
case ARRAY_SET: {
! if (objc != 4) {
! Tcl_WrongNumArgs(interp, 2, objv, "arrayName list");
return TCL_ERROR;
}
! return(TclArraySet(interp, objv[2], objv[3]));
}
case ARRAY_SIZE: {
Tcl_HashSearch search;
--- 3102,3113 ----
break;
}
case ARRAY_SET: {
! if ((objc != 4) && (objc != 5)) {
! Tcl_WrongNumArgs(interp, 2, objv, "arrayName list ?value?");
return TCL_ERROR;
}
! return TclArraySet(interp, objv[2], objv[3],
! ((objc == 4) ? NULL : objv[4]));
}
case ARRAY_SIZE: {
Tcl_HashSearch search;
***************
*** 3230,3240 ****
*/
int
! TclArraySet(interp, arrayNameObj, arrayElemObj)
Tcl_Interp *interp;/* Current interpreter. */
Tcl_Obj *arrayNameObj;/* The array name. */
Tcl_Obj *arrayElemObj;/* The array elements list. If this is
* NULL, create an empty array. */
{
Var *varPtr, *arrayPtr;
Tcl_Obj **elemPtrs;
--- 3231,3245 ----
*/
int
! TclArraySet(interp, arrayNameObj, arrayElemObj, arrayValueObj)
Tcl_Interp *interp;/* Current interpreter. */
Tcl_Obj *arrayNameObj;/* The array name. */
Tcl_Obj *arrayElemObj;/* The array elements list. If this is
* NULL, create an empty array. */
+ Tcl_Obj *arrayValueObj;/* If this is NULL, arrayElemObj holds
+ * index/element pairs. Otherwise arrayElemObj
+ * hold indices, and arrayValueObj holds
+ * the element value. */
{
Var *varPtr, *arrayPtr;
Tcl_Obj **elemPtrs;
***************
*** 3265,3285 ****
if (result != TCL_OK) {
return result;
}
! if (elemLen & 1) {
! Tcl_ResetResult(interp);
! Tcl_AppendToObj(Tcl_GetObjResult(interp),
! "list must have an even number of elements", -1);
! return TCL_ERROR;
! }
! if (elemLen > 0) {
! for (i = 0; i < elemLen; i += 2) {
! if (Tcl_ObjSetVar2(interp, arrayNameObj, elemPtrs[i],
! elemPtrs[i+1], TCL_LEAVE_ERR_MSG) == NULL) {
! result = TCL_ERROR;
! break;
}
}
! return result;
}
}
--- 3270,3303 ----
if (result != TCL_OK) {
return result;
}
! if (arrayValueObj == NULL) {
! if (elemLen & 1) {
! Tcl_ResetResult(interp);
! Tcl_AppendToObj(Tcl_GetObjResult(interp),
! "list must have an even number of elements", -1);
! return TCL_ERROR;
! }
! if (elemLen > 0) {
! for (i = 0; i < elemLen; i += 2) {
! if (Tcl_ObjSetVar2(interp, arrayNameObj, elemPtrs[i],
! elemPtrs[i+1], TCL_LEAVE_ERR_MSG) == NULL) {
! result = TCL_ERROR;
! break;
! }
}
+ return result;
}
! } else {
! if (elemLen > 0) {
! for (i = 0; i < elemLen; i++) {
! if (Tcl_ObjSetVar2(interp, arrayNameObj, elemPtrs[i],
! arrayValueObj, TCL_LEAVE_ERR_MSG) == NULL) {
! result = TCL_ERROR;
! break;
! }
! }
! return result;
! }
}
}
Index: tclEnv.c
===================================================================
RCS file: /cvsroot/tcl/generic/tclEnv.c,v
retrieving revision 1.6
diff -c -r1.6 tclEnv.c
*** tclEnv.c1999/10/29 03:04:001.6
--- tclEnv.c1999/11/23 17:54:11
***************
*** 101,107 ****
varNamePtr = Tcl_NewStringObj("env", -1);
Tcl_IncrRefCount(varNamePtr);
! TclArraySet(interp, varNamePtr, NULL);
Tcl_DecrRefCount(varNamePtr);
} else {
Tcl_MutexLock(&envMutex);
--- 101,107 ----
varNamePtr = Tcl_NewStringObj("env", -1);
Tcl_IncrRefCount(varNamePtr);
! TclArraySet(interp, varNamePtr, NULL, NULL);
Tcl_DecrRefCount(varNamePtr);
} else {
Tcl_MutexLock(&envMutex);
Index: tclInt.h
===================================================================
RCS file: /cvsroot/tcl/generic/tclInt.h,v
retrieving revision 1.37
diff -c -r1.37 tclInt.h
*** tclInt.h1999/11/10 02:51:561.37
--- tclInt.h1999/11/23 17:57:52
***************
*** 1543,1549 ****
EXTERN intTclAccessInsertProc _ANSI_ARGS_((TclAccessProc_ *proc));
EXTERN voidTclAllocateFreeObjects _ANSI_ARGS_((void));
EXTERN intTclArraySet _ANSI_ARGS_((Tcl_Interp *interp,
! Tcl_Obj *arrayNameObj, Tcl_Obj *arrayElemObj));
EXTERN intTclCleanupChildren _ANSI_ARGS_((Tcl_Interp *interp,
int numPids, Tcl_Pid *pidPtr,
Tcl_Channel errorChan));
--- 1543,1550 ----
EXTERN intTclAccessInsertProc _ANSI_ARGS_((TclAccessProc_ *proc));
EXTERN voidTclAllocateFreeObjects _ANSI_ARGS_((void));
EXTERN intTclArraySet _ANSI_ARGS_((Tcl_Interp *interp,
! Tcl_Obj *arrayNameObj, Tcl_Obj *arrayElemObj,
! Tcl_Obj *arrayValueObj));
EXTERN intTclCleanupChildren _ANSI_ARGS_((Tcl_Interp *interp,
int numPids, Tcl_Pid *pidPtr,
Tcl_Channel errorChan));
Index: set-old.test
===================================================================
RCS file: /cvsroot/tcl/tests/set-old.test,v
retrieving revision 1.7
diff -c -r1.7 set-old.test
*** set-old.test1999/09/21 04:20:451.7
--- set-old.test1999/11/23 17:55:36
***************
*** 446,455 ****
} {1 {"a" isn't an array}}
test set-old-8.31 {array command, set option} {
list [catch {array set a} msg] $msg
! } {1 {wrong # args: should be "array set arrayName list"}}
test set-old-8.32 {array command, set option} {
! list [catch {array set a 1 2} msg] $msg
! } {1 {wrong # args: should be "array set arrayName list"}}
test set-old-8.33 {array command, set option} {
list [catch {array set a "a \{ c"} msg] $msg
} {1 {unmatched open brace in list}}
--- 446,455 ----
} {1 {"a" isn't an array}}
test set-old-8.31 {array command, set option} {
list [catch {array set a} msg] $msg
! } {1 {wrong # args: should be "array set arrayName list ?value?"}}
test set-old-8.32 {array command, set option} {
! list [catch {array set a 1 2 3} msg] $msg
! } {1 {wrong # args: should be "array set arrayName list ?value?"}}
test set-old-8.33 {array command, set option} {
list [catch {array set a "a \{ c"} msg] $msg
} {1 {unmatched open brace in list}}
***************
*** 462,469 ****
catch {unset a}
set a(xx) yy
array set a {b c d e}
array get a
! } {d e xx yy b c}
test set-old-8.36 {array command, set option, array doesn't exist yet but has compiler-allocated procedure slot} {
proc foo {x} {
if {$x==1} {
--- 462,470 ----
catch {unset a}
set a(xx) yy
array set a {b c d e}
+ array set a {f {g h} i j} k
array get a
! } {d e xx yy i k j k f k b c {g h} k}
test set-old-8.36 {array command, set option, array doesn't exist yet but has compiler-allocated procedure slot} {
proc foo {x} {
if {$x==1} {
Index: array.n
===================================================================
RCS file: /cvsroot/tcl/doc/array.n,v
retrieving revision 1.4
diff -c -r1.4 array.n
*** array.n1999/09/21 04:20:351.4
--- array.n1999/11/23 17:55:02
***************
*** 83,99 ****
then all searches are automatically terminated just as if
\fBarray donesearch\fR had been invoked; this will cause
\fBarray nextelement\fR operations to fail for those searches.
.TP
! \fBarray set \fIarrayName list\fR
Sets the values of one or more elements in \fIarrayName\fR.
! \fIlist\fR must have a form like that returned by \fBarray get\fR,
! consisting of an even number of elements.
Each odd-numbered element in \fIlist\fR is treated as an element
name within \fIarrayName\fR, and the following element in \fIlist\fR
is used as a new value for that array element.
If the variable \fIarrayName\fR does not already exist
and \fIlist\fR is empty,
\fIarrayName\fR is created with an empty array value.
.TP
\fBarray size \fIarrayName\fR
Returns a decimal string giving the number of elements in the
--- 83,103 ----
then all searches are automatically terminated just as if
\fBarray donesearch\fR had been invoked; this will cause
\fBarray nextelement\fR operations to fail for those searches.
+ .VS 8.3
.TP
! \fBarray set \fIarrayName list\fR ?\fIvalue\fR?
Sets the values of one or more elements in \fIarrayName\fR.
! If \fIvalue\fR is omitted, \fIlist\fR must have a form like that
! returned by \fBarray get\fR, consisting of an even number of elements.
Each odd-numbered element in \fIlist\fR is treated as an element
name within \fIarrayName\fR, and the following element in \fIlist\fR
is used as a new value for that array element.
+ If \fIvalue\fR is provided, each element in \fIlist\fR is an element name
+ within \fIarrayName\fR that gets set to \fIvalue\fR.
If the variable \fIarrayName\fR does not already exist
and \fIlist\fR is empty,
\fIarrayName\fR is created with an empty array value.
+ .VE 8.3
.TP
\fBarray size \fIarrayName\fR
Returns a decimal string giving the number of elements in the
PatchFiles:
tclVar.c
tclEnv.c
tclInt.h
set-old.test
array.n
dkf Ticket [219315ffff]: 1 change
[History diff]2001-02-07 22:53:19
dgp Ticket [219315ffff]: 1 change
[History diff]2001-03-02 06:22:02
dgp Ticket [219315ffff]: 2 changes
[History diff]2001-04-04 04:49:32
dkf Ticket [219315ffff]: 2 changes
[History diff]2003-02-22 23:54:08
dkf Ticket [219315ffff]: 1 change
[History diff]2003-02-25 16:29:38
dkfplain2003-03-10 05:32:34
Logged In: YES user_id=79902 I'd prefer this as a separate subcommand. Maybe [array fill]?
dkf Ticket [219315ffff]: 1 change
[History diff]2009-07-16 16:57:31
ferrieuxplain2009-07-16 19:03:22
What is the advantage over the script version ? Speed ? Marginal I guess, and those fills are not often on the critical path of set algorithms. I am against random proliferation of such "primitives" (which are not). I vote for Reject.
dkfplain2009-07-16 19:34:36
Countering that argument is the fact that several people have asked for this. It's worth doing a TIP (after all, it's "mostly harmless") and seeing if other people will agree with either argument.
Log in (anonymous is fine) to add a comment.