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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting results from a Loop

Status
Not open for further replies.

XfrankX

IS-IT--Management
Aug 12, 2011
2
NL
Hi all,

I'm a little new to TCL, and maybe it's VERY simple (which I guess it is), but I can't seem to figure it out, here's my problem:

I got a loop which outputs all companies in a game with their current value, the loop code can be found in the attachment...

The output is like this:

#1 is Example Transport (Red) with a company value of: 75620
#2 is Example Transport2 (Green) with a company value of: 632689

Now, the list can be pretty big offcourse depending on the number of companies / players in the game, but what I would like to do is to make a high score based on company value, so I would like to sort on company value which puts the company with the biggest company value on top.

Could someone please point me in the right direction?

Thanks

Frank


 
The problem is, you have an array, not a list. A list is easy to sort, an array, not so much. The best thing to do would be to rearrange your code so you have a list of lists, not an array of lists. Then you could "lsort -index 5 <listname>".

You can make an array into a list but you'll have pesky numbers interspersed:
Code:
% set a(1) {a b c}
a b c
% set a(2) {d e f}
d e f
% array get a
1 {a b c} 2 {d e f}
%

You can get around that by selecting every other element:
Code:
% for {set i 1} {$i<[llength $lsta]} {incr i 2} {lappend lstb [lindex $lsta $i]}
% set lstb
{a b c} {d e f}
%
and then sort with -index 5

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top