This is one problem I had, but completely worked around it,
by changing it to nested lists (and lsort -index ...).
Nevertheless, I'm curious, what would be the best approach to
get from outset:
set list {{key 42 a 1} {key 54 b 5} {c 6 key 30}}
to the list sorted by the value for "key" from the nested dicts:
{c 6 key 30} {key 42 a 1} {key 54 b 5}
So far I can only think of lsort -command ...
On Thursday, January 27, 2022 at 6:48:23 PM UTC+1, Andreas Leitgeb wrote:
This is one problem I had, but completely worked around it,
by changing it to nested lists (and lsort -index ...).
Nevertheless, I'm curious, what would be the best approach to
get from outset:
set list {{key 42 a 1} {key 54 b 5} {c 6 key 30}}
to the list sorted by the value for "key" from the nested dicts:
{c 6 key 30} {key 42 a 1} {key 54 b 5}
So far I can only think of lsort -command ...
Not an answer, but sharing my experience. I mostly avoid lists of
dicts and prefer dicts of dicts by using the key in the outer-most
structure, i.e.
set data {42 {key 42 a 1} 54 {key 54 b 5} 30 {c 6 key 30}}
I assume that [dict keys] is quite cheap as long as $data does not
shimmer ... forearch key [lsort [dict keys $data]] {set record [dict
get $data $key]; ...}
heinrichmartin <martin.heinrich@frequentis.com> wrote:
On Thursday, January 27, 2022 at 6:48:23 PM UTC+1, Andreas Leitgeb wrote: >>> This is one problem I had, but completely worked around it,
by changing it to nested lists (and lsort -index ...).
Nevertheless, I'm curious, what would be the best approach to
get from outset:
set list {{key 42 a 1} {key 54 b 5} {c 6 key 30}}
to the list sorted by the value for "key" from the nested dicts:
{c 6 key 30} {key 42 a 1} {key 54 b 5}
So far I can only think of lsort -command ...
Not an answer, but sharing my experience. I mostly avoid lists of
dicts and prefer dicts of dicts by using the key in the outer-most
structure, i.e.
set data {42 {key 42 a 1} 54 {key 54 b 5} 30 {c 6 key 30}}
I assume that [dict keys] is quite cheap as long as $data does not
shimmer ... forearch key [lsort [dict keys $data]] {set record [dict
get $data $key]; ...}
Thanks!
Very good point!
Compared to [lsort -command ...], even an extra iteration to turn the
outer list to a dict would have been a breeze...
Not an answer neither. If you arange to get key at the first position
always, you can use lsort -index 1.
My big data structures are normally array of dicts, to:
- restrict shimmering on the toplevel key level (array elements does not shimmer the whole array)
- then use highly nested dicts to have "dict index a(b) $i1 $i2 $i3".
To some degree, arrays may do more shimmering harm than dicts...
in particular the keys for arrays are always shimmered to string,
unlike for dicts... - at least that's what I've gathered here in
the past. Not entirely sure, if still true.
Am 28.01.2022 um 09:34 schrieb Andreas Leitgeb:
To some degree, arrays may do more shimmering harm than dicts...
in particular the keys for arrays are always shimmered to string,
unlike for dicts... - at least that's what I've gathered here in
the past. Not entirely sure, if still true.
That is an important point, but I suppose, this is also true for dicts.
They both use a hash-table for internal organisation and the hash-table requires always a string representation.
Harald Oehlmann <wortkarg2@yahoo.de> wrote:
Am 28.01.2022 um 09:34 schrieb Andreas Leitgeb:
To some degree, arrays may do more shimmering harm than dicts...
in particular the keys for arrays are always shimmered to string,
unlike for dicts... - at least that's what I've gathered here in
the past. Not entirely sure, if still true.
That is an important point, but I suppose, this is also true for dicts.
They both use a hash-table for internal organisation and the hash-table
requires always a string representation.
There is a subtle difference between "shimmering to string" and merely obtaining a string for the hash and then not storing the string back
into the object.
Adding a string-rep to an object is not always an improvement.
Existing string-reps sometimes inhibit alternative "direct routes"
from some object to another.
e.g.: Given a dict object with a string-rep, it is possible, that the string-rep was originally a list's string-rep that still contains
duplicate keys in the dict-sense, but these are perfectly legal for
a list. Therefore, the list would be re-parsed from the string rep
rather than from the dict-rep.
Without a string-rep, the list can (or at least could) be directly
obtained from the dict.
Adding a string-rep to an object is not always an improvement.
Existing string-reps sometimes inhibit alternative "direct routes"
from some object to another.
e.g.: Given a dict object with a string-rep, it is possible, that the string-rep was originally a list's string-rep that still contains
duplicate keys in the dict-sense, but these are perfectly legal for
a list. Therefore, the list would be re-parsed from the string rep
rather than from the dict-rep.
Without a string-rep, the list can (or at least could) be directly
obtained from the dict.
set defaults [dict create foo 1 bar 2];list
% tcl::unsupported::representation $defaults
On Friday, January 28, 2022 at 11:17:14 AM UTC+1, Andreas Leitgeb wrote:
Adding a string-rep to an object is not always an improvement.
Existing string-reps sometimes inhibit alternative "direct routes"
from some object to another.
e.g.: Given a dict object with a string-rep, it is possible, that the
string-rep was originally a list's string-rep that still contains
duplicate keys in the dict-sense, but these are perfectly legal for
a list. Therefore, the list would be re-parsed from the string rep
rather than from the dict-rep.
Without a string-rep, the list can (or at least could) be directly
obtained from the dict.
One never stops learning. This means it actually makes a difference whether I start a dict with [dict create] or not (namely no string rep vs string rep).
On the other hand, I can still stick to initialising short constant dicts with braces (rather than [dict create]), because it will not be measurable in e.g.,
set defaults {foo 1 bar 2} ;# that is imho more readable than [dict create foo 1 bar 2]
On Friday, January 28, 2022 at 11:17:14 AM UTC+1, Andreas Leitgeb wrote:
Adding a string-rep to an object is not always an improvement.
One never stops learning. This means it actually makes a difference
whether I start a dict with [dict create] or not (namely no string rep
vs string rep).
Now without string interpretation:
Remark the funny ";list" at the end of the command to avoid to put the
result as command result, which then would be converted by the sehll to
a string for printing, which would add a string representation
% set defaults [dict create foo 1 bar 2];list
% tcl::unsupported::representation $defaults
value is a dict with a refcount of 7, object pointer at 007D4430,
internal representation 040879E0:00000000, string representation "foo 1
bar 2"
Harald Oehlmann wrote:
Now without string interpretation:
Remark the funny ";list" at the end of the command to avoid to put the result as command result, which then would be converted by the sehll to
a string for printing, which would add a string representation
% set defaults [dict create foo 1 bar 2];listI was surprised, that there even is a string rep in that case, so I
% tcl::unsupported::representation $defaults
value is a dict with a refcount of 7, object pointer at 007D4430,
internal representation 040879E0:00000000, string representation "foo 1
bar 2"
checked that I could reproduce it (yes, I could), and then tried
another one:
% set defaults [dict create foo [expr {1+1}] bar 4];list
% tcl::unsupported::representation $defaults
value is a dict with a refcount of 2, object pointer at 0x55d874107190, internal representation 0x55d874172b60:(nil), no string representation
Apparently, there exists a special case, where all key and value
elements of a new dict are all strings themselves, and then a string-rep
of the dict is (unnecessarily?) pre-generated.
Harald Oehlmann <wort...@yahoo.de> wrote:
Now without string interpretation:
Remark the funny ";list" at the end of the command to avoid to put the result as command result, which then would be converted by the sehll to
a string for printing, which would add a string representation
% set defaults [dict create foo 1 bar 2];listI was surprised, that there even is a string rep in that case, so I
% tcl::unsupported::representation $defaults
value is a dict with a refcount of 7, object pointer at 007D4430,
internal representation 040879E0:00000000, string representation "foo 1 bar 2"
checked that I could reproduce it (yes, I could), and then tried
another one:
% set defaults [dict create foo [expr {1+1}] bar 4];list
% tcl::unsupported::representation $defaults
value is a dict with a refcount of 2, object pointer at 0x55d874107190, internal representation 0x55d874172b60:(nil), no string representation
Apparently, there exists a special case, where all key and value
elements of a new dict are all strings themselves, and then a string-rep
of the dict is (unnecessarily?) pre-generated.
Your special case seems to be crafted by the interactive mode -
not even non-normalized string reps in the command could break that.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 463 |
Nodes: | 16 (2 / 14) |
Uptime: | 157:00:27 |
Calls: | 9,384 |
Calls today: | 4 |
Files: | 13,561 |
Messages: | 6,095,919 |