#3430 lsort inconsistant behavior for negative value of -index

obsolete: 8.4.11
open
Jeffrey Hobbs
5
2014-08-24
2006-05-08
dansic
No

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"

Discussion

  • 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.

     
    • labels: 105658 --> 10. Objects
    • assigned_to: dkf --> hobbs