Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

lsort and escaping backslash

Status
Not open for further replies.

raghavs81

IS-IT--Management
Sep 27, 2006
1
US
I have the following set of code

lappend to_be_sorted_list {\\\\ABCD 3} {EFG 7} {XYZ 4}
set sorted_list [lsort -decreasing -command -integer inst_length_compare $to_be_sorted_list]
foreach elt $sorted_list {
puts $elt
}

proc inst_length_compare {a b} {
set alength [lindex $a [expr {[llength $a] - 1}]]
set blength [lindex $b [expr {[llength $b] - 1}]]
set diff [expr {$alength - $blength}]
return $diff
}


The output of this proc is

EFG 7
XYZ 4
\\ABCD 3

As you can see 2 of the backslaches in the element "ABCD" disappears in the sorted list. I guess this is probably because '\' is treated as an escape character and lsort removes it. Is there any way to avoid this and have all the 4 backslashes for my "ABCD" element in my sorted list.
 
The lappend and lsort functions themeselves maintain the 4 slashes. Likewise the puts.

But lindex {\\\\ABCD 3} 0 returns \\ABCD, so it's happening when you pass the elements into the proc.

However, lindex {{\\\\ABCD} 3} 0 returns \\\\ABCD so if possible, I think
lappend to_be_sorted_list {\\\\ABCD 3} {EFG 7} {XYZ 4}
should be
lappend to_be_sorted_list {{\\\\ABCD} 3} {EFG 7} {XYZ 4}


_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top