Skip to content
Tcl Source Code View Ticket
Login

View Ticket

lsort inconsistant behavior for negative value of -index

Closed / Fixed1483802fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 41 days ago
Descriptionplain2006-05-08 13:31:01
Platform: Windows XP

When a negative value is given to the -index option of
lsort inconsistant behavior do occur.

The man page for lsort do not discuss behavior for
negative value of -index.  One would expect either an
empty string being returned or preferably an error be
throwned as when the index is out of range.

For some negative value a sorted list is return but it
seems that the stable property of lsort is lost.

For some other value a list is returned but whitout any
discernible sorting.

Finally for some others value an error is throwned. 
The error is the same as for an out of range positive
value for -index.

Example:

(bin) 94 % set list2 {{5 4 6} {10 12 11} {2 3 1} {5 7 1}}
{5 4 6} {10 12 11} {2 3 1} {5 7 1}
(bin) 95 % lsort -dictionary -index 2 $list2
{2 3 1} {5 7 1} {5 4 6} {10 12 11}
(bin) 96 % lsort -dictionary -index -2 $list2
{2 3 1} {5 7 1} {5 4 6} {10 12 11}
(bin) 97 % lsort -dictionary -index -3 $list2
{2 3 1} {5 4 6} {5 7 1} {10 12 11}
(bin) 98 % lsort -dictionary -index 3 $list2
element 3 missing from sublist "5 4 6"
(bin) 99 % lsort -dictionary -index -4 $list2
{2 3 1} {5 4 6} {5 7 1} {10 12 11}
(bin) 100 % lsort -dictionary -index -5 $list2
element -5 missing from sublist "5 4 6"
(bin) 101 % lsort -dictionary -index -1 $list2
{2 3 1} {5 4 6} {5 7 1} {10 12 11}


(bin) 104 % set list3 {{5 2 3 4 3} {3 6 9 2 7} {5 1 12
13 15} {9 3 34 12 3} {1 3 32 24 5} {4 9 3 6 5}}
{5 2 3 4 3} {3 6 9 2 7} {5 1 12 13 15} {9 3 34 12 3} {1
3 32 24 5} {4 9 3 6 5}
(bin) 105 % lsort -dictionary -index 0 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 2 3 4 3} {5 1
12 13 15} {9 3 34 12 3}
(bin) 106 % lsort -dictionary -index 1 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{3 6 9 2 7} {4 9 3 6 5}
(bin) 107 % lsort -dictionary -index -1 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 1 12 13 15} {5
2 3 4 3} {9 3 34 12 3}
(bin) 108 % lsort -dictionary -index 2 $list3
{5 2 3 4 3} {4 9 3 6 5} {3 6 9 2 7} {5 1 12 13 15} {1 3
32 24 5} {9 3 34 12 3}
(bin) 109 % lsort -dictionary -index -2 $list3
{5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5} {4 9 3 6 5} {3
6 9 2 7} {5 1 12 13 15}
(bin) 110 % lsort -dictionary -index 3 $list3
{3 6 9 2 7} {5 2 3 4 3} {4 9 3 6 5} {9 3 34 12 3} {5 1
12 13 15} {1 3 32 24 5}
(bin) 111 % lsort -dictionary -index -3 $list3
{3 6 9 2 7} {5 2 3 4 3} {4 9 3 6 5} {9 3 34 12 3} {5 1
12 13 15} {1 3 32 24 5}
(bin) 112 % lsort -dictionary -index 4 $list3
{5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5} {4 9 3 6 5} {3
6 9 2 7} {5 1 12 13 15}
(bin) 113 % lsort -dictionary -index -4 $list3
{5 2 3 4 3} {4 9 3 6 5} {3 6 9 2 7} {5 1 12 13 15} {1 3
32 24 5} {9 3 34 12 3}
(bin) 114 % lsort -dictionary -index 5 $list3
element 5 missing from sublist "5 2 3 4 3"
(bin) 115 % lsort -dictionary -index -5 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{3 6 9 2 7} {4 9 3 6 5}
(bin) 116 % lsort -dictionary -index 6 $list3
element 6 missing from sublist "5 2 3 4 3"
(bin) 117 % lsort -dictionary -index -6 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 2 3 4 3} {5 1
12 13 15} {9 3 34 12 3}
(bin) 118 % lsort -dictionary -index 7 $list3
element 7 missing from sublist "5 2 3 4 3"
(bin) 119 % lsort -dictionary -index -7 $list3
element -7 missing from sublist "5 2 3 4 3"

(bin) 120 % lsort -index 0 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 2 3 4 3} {5 1
12 13 15} {9 3 34 12 3}
(bin) 121 % lsort -index 1 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{3 6 9 2 7} {4 9 3 6 5}
(bin) 122 % lsort -index -1 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 1 12 13 15} {5
2 3 4 3} {9 3 34 12 3}
(bin) 123 % lsort -index 2 $list3
{5 1 12 13 15} {5 2 3 4 3} {4 9 3 6 5} {1 3 32 24 5} {9
3 34 12 3} {3 6 9 2 7}
(bin) 124 % lsort -index -2 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{4 9 3 6 5} {3 6 9 2 7}
(bin) 125 % lsort -index 3 $list3
{9 3 34 12 3} {5 1 12 13 15} {3 6 9 2 7} {1 3 32 24 5}
{5 2 3 4 3} {4 9 3 6 5}
(bin) 126 % lsort -index -3 $list3
{9 3 34 12 3} {5 1 12 13 15} {3 6 9 2 7} {1 3 32 24 5}
{5 2 3 4 3} {4 9 3 6 5}
(bin) 127 % lsort -index 4 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{4 9 3 6 5} {3 6 9 2 7}
(bin) 128 % lsort -index -4 $list3
{5 1 12 13 15} {5 2 3 4 3} {4 9 3 6 5} {1 3 32 24 5} {9
3 34 12 3} {3 6 9 2 7}
(bin) 129 % lsort -index 5 $list3
element 5 missing from sublist "5 2 3 4 3"
(bin) 130 % lsort -index -5 $list3
{5 1 12 13 15} {5 2 3 4 3} {9 3 34 12 3} {1 3 32 24 5}
{3 6 9 2 7} {4 9 3 6 5}
(bin) 131 % lsort -index 6 $list3
element 6 missing from sublist "5 2 3 4 3"
(bin) 132 % lsort -index -6 $list3
{1 3 32 24 5} {3 6 9 2 7} {4 9 3 6 5} {5 2 3 4 3} {5 1
12 13 15} {9 3 34 12 3}
(bin) 133 % lsort -index 7 $list3
element 7 missing from sublist "5 2 3 4 3"
(bin) 134 % lsort -index -7 $list3
element -7 missing from sublist "5 2 3 4 3"
mic42plain2006-08-02 15:11:12
Logged In: YES 
user_id=302287

This is an artifact from using TclGetIntFromIndex() on the
index, which uses negative indices to indicate an end
relative index.

As TclGetIntFromIndex() allows negative values for indices
this is would probably either require a specialized
TclGetIntFromIndex() which guards against negative indices
other than end relative indices..., or one extra call to
Tcl_GetIntFromObj() first (which is also done at the
beginning of TclGetIntFromIndex()), with a check for
negative values.
dkf Ticket [1483802fff]: 2 changes [History diff]2006-08-04 00:16:05
serhiy.storchakawiki2026-10-21 10:03:37

Fixed in 8.6.9 by [71c0a83836] (ticket [db36fa5122]): negative -index values now raise an error instead of being taken as end-relative.

serhiy.storchaka Ticket [1483802fff]: 3 changes [History diff]2026-10-22 17:19:30
Log in (anonymous is fine) to add a comment.