I have an array with dynamic 'keys' and values associated with them.
I want to sort the array based on the value and want to be able to retrieve the 'keys' from the sorted array.
For example, say I have,
for {set i 0} {$i < [db_get_nrows $rs]} {incr i} {
set x [db_get_col $rs $i abc]
set ARRAY_A($x) [db_get_col $rs $i def]
}
So, my array would look like,
ARRAY_A(111) 10
ARRAY_A(222) 50
ARRAY_A(333) 20
Now, I want to sort this array based on it's values (with 50 first, then 20 and then 10). And then I'm interested in it's keys (222, 333 and 111) for further processing.
I couldn't find much in the internet for such arrays with dynamically generated keys.
Any help is much appreciated.
Thanks.
Well, I just wanted to first mention that you cannot sort arrays as they don't really have a fixed order, but are saved in a manner that makes it easier/faster for the interpreter to retrieve values.
If you want to get the keys of the array in the order of the values, you can maybe use something like that:
set key_value [lmap {key val} [array get ARRAY_A] {list $key $val}]
set key_value [lsort -index 1 -integer -decreasing $key_value]
The list key_value now holds key/value pairs of your array sorted by values in decreasing order. -index 1 indicates that the sort is sorting by the 2nd element of the sublist (Tcl has lists 0-based). -integer just instructs that we are sorting integers (and not using dictionary sort). You just need to get the keys from the list:
foreach n $key_value {
puts [lindex $n 0]
}
You can combine the above in a single loop if you want (I combined the loop and the second line, adding the first line will make it look a bit too much):
foreach n [lsort -index 1 -integer -decreasing $key_value] {
puts [lindex $n 0]
}
This part of the answer is mostly an addendum to Dinesh's answer and not complete in itself.
Once you have created a list containing the array elements sorted according to value, you can put it in a dictionary (which is another kind of associative list structure):
set d [lsort -stride 2 -integer -decreasing -index 1 $l]
The dictionary will preserve the order of insertion and allow easy access to e.g. the keys:
dict keys $d
# -> 222 333 111
eta
If you can't use lmap or -stride, you can still generate a dictionary like this:
set pairs {}
foreach {a b} [array get ARRAY_A] {
lappend pairs [list $a $b]
}
set DICT_A [concat {*}[lsort -index 1 -integer -decreasing $pairs]]
This method packs the elements into "pairs", sorts the packed list, and then unpacks it into a flat list to be usable as a dictionary as above.
Documentation: array, concat, dict, foreach, lappend, list, lsort, set
% set tcl_version
8.6
% array set n {111 10 222 50 333 20}
% parray n
n(111) = 10
n(222) = 50
n(333) = 20
% set l [array get n]
333 20 222 50 111 10
% lsort -stride 2 -integer -index 1 $l
111 10 333 20 222 50
% lsort -stride 2 -integer -decreasing -index 1 $l
222 50 333 20 111 10
%
You can get them as list with expected order and then try to apply your logic further.
Related
how to sort an Array output for example
Sample input coming from
puts "$word $count($word)"}
Sample Input
Roger 15
Martin 18
Jemmy 16
Jon 12
Sara 12
Expected Output
Martin 18
Jemmy 16
Roger 15
Jon 12
Sara 12
Tcl's arrays are unsorted, always, and indeed the order of the elements changes from time to time as you add elements in (when the underlying hash table is rebuilt). To get the output you want, you're best off getting the contents of the array and using lsort with the -stride 2 option:
# Convert the array to a Tcl list
set contents [array get count]
# First sort by name, as a secondary key
set contents [lsort -stride 2 -index 0 $contents]
# Then sort by count, descending, as a primary key
set contents [lsort -stride 2 -index 1 -integer -decreasing $contents]
# Print the values
foreach {name score} $contents {
puts "$name $score"
}
The -stride option requires Tcl 8.6.
In older versions of Tcl, you have to pack things up into a list of tuples:
# Convert the array to a list of pairs
set contents {}
foreach {name score} [array get count] {
lappend contents [list $name $score]
}
# Do the sorting
set contents [lsort -index 0 $contents]
set contents [lsort -index 1 -integer -decreasing $contents]
# Print the values
foreach pair $contents {
# Unpack; *not* needed here, but useful for anything more complicated
foreach {name score} $pair break
# You could use “lassign $pair name score” but you're on 8.4
puts "$name $score"
}
Note that Tcl 8.4 is unsupported software, not even for security issues, and that 8.5 has only got a year or two more extended support lifetime left. There's a limit to how long we'll hold people's hands…
You probably have something like this
array set count { Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12 }
foreach word [array names count] {puts "$word $count($word)"}
Jemmy 16
Sara 12
Jon 12
Martin 18
Roger 15
what you want to do is to transform the array into a list, step over it in pairs and sort the pairs based on the number:
foreach {name num} \
[lsort -integer -decreasing -stride 2 -index 1 [array get count]] \
{puts "$name $num"}
Martin 18
Jemmy 16
Roger 15
Sara 12
Jon 12
refs:
http://www.tcl.tk/man/tcl8.6/TclCmd/lsort.htm
http://www.tcl.tk/man/tcl8.6/TclCmd/foreach.htm
Solution for Tcl < 8.6:
Given
array set count {Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12}
The way to get sorted outpus is
set L [list]
foreach {k v} [array get count] {
lappend L [list $k $v]
}
foreach e [lsort -index 1 -decreasing -integer $L] {
lassign $e k v
puts "$k $v"
}
Explanation:
Get a flat list of the interleaved keys and values using array get.
From it, produce a list of key/value pairs—a list of lists.
Given that list, sort it using the lsort command passing it
the -index 1 option which makes lsort interpret the elements of the
list it sorts as lists, and use their elements at index 1
(the 2nd position) for sorting.
To print out the elements of the sorted list, you need to extract the
key and the value back from each of them. The easiest way is to use
lassign but if you have Tcl < 8.5 you can use either the
foreach {k v} $e break trick or directly access the elements using
lindex $e 0 and lindex $e 1 to get the key and the value, respectively.
I have an array with many fields in each line spaced by different spacing like:
INDDUMMY drawing2 139 30 1 0 0 0 0 0
RMDUMMY drawing2 69 2 1 0 0 0 0 0
PIMP drawing 7 0 1444 718 437 0 0 0
I'm trying to make sorting for this array by number in 3rd field so the desired output should be:
PIMP drawing 7 0 1444 718 437 0 0 0
RMDUMMY drawing2 69 2 1 0 0 0 0 0
INDDUMMY drawing2 139 30 1 0 0 0 0 0
I tried to make a split using regular expression within the sorting function like:
#sortedListOfLayers = sort {
split(m/\w+\s+(\d+)\s/gm,$a)
cmp
split(m/\w+\s+(\d+)\s/gm,$b)
}#listOfLayers;
but it doesn't work correctly. How I could make that type of sorting?
You need to expand out your sort function a little further. I'm also not sure that split is working the way you think it is. Split turns text into an array based on a delimiter.
I think your problem is that your regular expression - thanks to the gm flags - isn't matching what you think it's matching. I'd perhaps approach it slightly differently though:
#!/usr/bin/perl
use strict;
use warnings;
my #array = <DATA>;
sub sort_third_num {
my $a1 = (split ( ' ', $a ) )[2];
my $b1 = (split ( ' ', $b )) [2];
return $a1 <=> $b1;
}
print sort sort_third_num #array;
__DATA__
NDDUMMY drawing2 139 30 1 0 0 0 0 0
RMDUMMY drawing2 69 2 1 0 0 0 0 0
PIMP drawing 7 0 1444 718 437 0 0 0
This does the trick, for example.
If you're set on doing a regex approach:
sub sort_third_num {
my ($a1) = $a =~ m/\s(\d+)/;
my ($b1) = $b =~ m/\s(\d+)/;
return $a1 <=> $b1;
}
not globally matching means only the first element is returned. And only the first match of 'whitespace-digits' is returned. We also compare numerically, rather than stringwise.
If you want to sort a list and the operation used in the sort block is expensive, an often used Perl idiom is the Schwartzian Transform: you apply the operation once to each list element and store the result alongside the original element, sort, then map back to your original format.
The classic textbook example is sorting files in a directory by size using the expensive -s file test. A naïve approach would be
my #sorted = sort { -s $a <=> -s $b } #unsorted;
which has to perform -s twice for each comparison operation.
Using the Schwartzian Transform, we map the file names into a list of array references, each referencing an array containing the list element and its size (which has to be determined only once per file), then sort by file size, and finally map the array references back to just the file names. This is all done in a single step:
my #sorted =
map $_->[0], # 3. map to file name
sort { a$->[1] <=> b$->[1] } # 2. sort by size
map [ $_, -s $_ ], # 1. evaluate size once for each file
#unsorted;
In your case, the question is how expensive it is to extract the third field of each array element. When in doubt, measure to compare different methods. The speedup in the file size example is dramatic at about a factor 10 for a few dozen files!
The Schwartzian Transform applied to your problem would look something like this:
my #sorted =
map $_->[0], # 3. Map to original array
sort { $a->[1] <=> $b->[1] } # 2. Sort by third column
map [ $_, ( split( ' ', $_ ) )[2] ], # 1. Use Sobrique's idea
#array;
If the operation used is so expensive that you want to avoid performing it more than once per value in case you have identical array elements, you can cache the results as outlined in this question; this is known as the Orcish Maneuver.
i have strings inside an array in this way
/hello/Stack/oveflow 14
/hello/Stack/oveflow 11
/hello/Stack/oveflow 12
/hello/Stack/oveflow 166
/hello/Stack/oveflow 1
/hello/Stack/oveflow 2
/hello/Stack/oveflow 5
i have to sort by the last number
is it possible to use sort to do that?
Yes, sort is exactly what you need. Just provide the code block to compare two elements:
my #sorted = sort { ($a =~ /[0-9]+/g)[-1]
<=>
($b =~ /[0-9]+/g)[-1]
} #array;
<=> does the numeric comparison. The matching returns all the numbers in the string, [-1] selects the last one.
The INITIAL_ARRAY is
Key -> Value
B 8
C 10
A 5
E 3
D 1
To get a sorted array based on key, I use
set sorted_keys_array [lsort [array names INITIAL_ARRAY]]
to get the output
Key -> Value
A 5
B 8
C 10
D 1
E 3
Like wise, how to get a sorted tcl array based on values of keys, like output below?
Key -> Value
C 10
B 8
A 5
E 3
D 1
Starting with Tcl 8.6, you could do
lsort -stride 2 -integer [array get a]
which would produce a flat list of key/value pairs sorted on values.
Before lsort gained the -stride option, you had to resort to constructing a list of lists out of the flat list array get returns and then sort it using the -index option for lsort:
set x [list]
foreach {k v} [array get a] {
lappend x [list $k $v]
}
lsort -integer -index 1 $x
The previous methods on this didn't work for me, when I have an array:
[array get a] == D 1 E 3 A 5 B 8 C 10
I do a the following and receive the error:
lsort -stride 2 -integer [array get a]
expected integer but got "D"
You need to also add in an index:
lsort -integer -stride 2 -index 1 [array get a]
D 1 C 10 E 3 A 5 B 8
And then you can change the direction:
lsort -decreasing -integer -stride 2 -index 1 [array get a]
C 10 B 8 A 5 E 3 D 1
Which then gives the correct answer
I wanted to create a list of numbers with missing numbers in a given list as provided in the example below
Existing list { 1,3, 5, 9 , 13, 15}
Resultant list { 2,4,6,7,8,10,11,12,14}
Extended TCL has the function intersect3 which as one of its return values gives a list of A-B. You could intersect your list with a list of all possible numbers that span your list.
If you don't use Extended TCL, you'll have to implement something yourself.
I hardly ever use TCL, so maybe there's a better way, but the basic approach is to just sort the list, then run through it and find the missing values:
#!/usr/bin/tclsh
set A {1 3 5 9 13 15}
set A [lsort -integer $A]
set B {}
set x 0
set y [lindex $A $x]
while {$x < [llength $A]} {
set i [lindex $A $x]
while {$y < $i} {
lappend B $y
incr y
}
incr x
incr y
}
puts $B
Output:
2 4 6 7 8 10 11 12 14
paddy's answer looks pretty good. This is a bit shorter, assumes the list is already sorted.
package require Tcl 8.5
set A {1 3 5 9 13 15}
set result [list]
for {set i [lindex $A 0]; incr i} {$i < [lindex $A end]} {incr i} {
if {$i ni $A} {
lappend result $i
}
}
See http://tcl.tk/man/tcl8.5/TclCmd/expr.htm#M15 for the "ni" operator.