Drektz
Drektz

Reputation: 95

Why does lsort remove '\' from the list elements?

lsort removes '\' from the list elements

set a [regsub -all {\d+} [list temp25 temp36] {\d+}]
puts $a
>>temp\d+ temp\d+
puts [lsort -u $a]
>>tempd+

I know the workaround to get desire result. This question is more about asking whether this behavior is expected? According to my understanding, lsort should not change the elements at any cost.

Upvotes: 0

Views: 122

Answers (2)

Alex P
Alex P

Reputation: 96

Probably, A. Richard meant that split and lsort might have the same behavior as for a string being passed as an argument. But they don't:

set a "{temp\\d+ temp\\d+} {temp\\d+ temp\\d+} temp\\d+ temp\\d+"
puts [split $a]
puts [lsort $a]

... and probably should not, as split gets a string, while lsort gets a list as input. If lsort gets a string, it converts the string into a list firstly, making the appropriate substitutions. Hence the issue ("\d" doesn't exist, so only "d" remains of it).

I.e., lsort performs firstly:

list {*}$a

and then sorts the result.

Upvotes: 0

mrcalvin
mrcalvin

Reputation: 3434

This question is more about asking whether this behavior is expected?

Yes. regsub operates and produces a string, while lsort works on a Tcl list. If you submit an arbitrary string to a list operation, then it might not get interpreted as expected. So, therefore, if you want to have a string (maybe superficially looking like a list) treated also as one, then you have to turn it into one explicitly using split.

Watch:

% set a {temp\d+ temp\d+}
temp\d+ temp\d+
% split $a
{temp\d+} {temp\d+}
% lsort -u [split $a]
{temp\d+}

If not properly sanitized, then characters special to Tcl like backslash or brackets will be processed as such. This is like:

% set _ temp\d+
tempd+
% # versus
% set _ {temp\d+}
temp\d+

Upvotes: 1

Related Questions