Skip to main content
added 362 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do something like:

typeset -A assoc
assoc=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)
ordered_keys=()

for v ("${(@nO)assoc}") ordered_keys+=("${(@k)assoc[(eR)$v]}")

That is, order (O) the list of values ($assoc) numerically (n) and for each value, add the matching key(s) (e for exact match, R to get the reverse list based on value, not key) and add that to the ordered_keys array.

See info zsh flags on your system¹ (or the same online for the latest version of zsh) for details about those parameter expansion flags.


¹ Note on some systems, you'll also need to install a zsh-doc package to get the documentation in info format.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do something like:

typeset -A assoc
assoc=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)
ordered_keys=()

for v ("${(@nO)assoc}") ordered_keys+=("${(@k)assoc[(eR)$v]}")

That is, order (O) the list of values ($assoc) numerically (n) and for each value, add the matching key(s) (e for exact match, R to get the reverse list based on value, not key) and add that to the ordered_keys array.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do something like:

typeset -A assoc
assoc=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)
ordered_keys=()

for v ("${(@nO)assoc}") ordered_keys+=("${(@k)assoc[(eR)$v]}")

That is, order (O) the list of values ($assoc) numerically (n) and for each value, add the matching key(s) (e for exact match, R to get the reverse list based on value, not key) and add that to the ordered_keys array.

See info zsh flags on your system¹ (or the same online for the latest version of zsh) for details about those parameter expansion flags.


¹ Note on some systems, you'll also need to install a zsh-doc package to get the documentation in info format.

Post Undeleted by Stéphane Chazelas
deleted 460 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

A shell is before all a tool to run other tools. It sounds to me you're after a programming language like perl, ruby, python...

Having said that, here is some possible solution for zsh.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do thingssomething like:

typeset -A A Bassoc
A=assoc=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)
ordered_keys=()

for v ("${(@nO)Aassoc}") B+=ordered_keys+=("${(@kv@k)A[assoc[(eR)$v]}")

That is, order (O) the list of values ($A$assoc) numerically (n) and for each value, add the matching key/value pairs matching the value $v (s) (e for exact match, R to get the reverse list based on value, not key) and add that to the Bordered_keys associative array.

Then, you'd get the sorted list in B:

$ printf '%s => %s\n' "${(@kv)B}"
192.168.8.1 => 9
192.168.1.1 => 9
192.168.2.2 => 5
192.168.3.2 => 1

And you can select the first 2 keys with

$ print -rl -- ${${(k)B}[1,2]}
192.168.8.1
192.168.1.1

A shell is before all a tool to run other tools. It sounds to me you're after a programming language like perl, ruby, python...

Having said that, here is some possible solution for zsh.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do things like:

typeset -A A B
A=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)

for v ("${(@nO)A}") B+=("${(@kv)A[(eR)$v]}")

That is, order (O) the list of values ($A) numerically (n) and for each value, add the key/value pairs matching the value $v (e for exact match, R to get the reverse list based on value, not key) and add that to the B associative array.

Then, you'd get the sorted list in B:

$ printf '%s => %s\n' "${(@kv)B}"
192.168.8.1 => 9
192.168.1.1 => 9
192.168.2.2 => 5
192.168.3.2 => 1

And you can select the first 2 keys with

$ print -rl -- ${${(k)B}[1,2]}
192.168.8.1
192.168.1.1

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do something like:

typeset -A assoc
assoc=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)
ordered_keys=()

for v ("${(@nO)assoc}") ordered_keys+=("${(@k)assoc[(eR)$v]}")

That is, order (O) the list of values ($assoc) numerically (n) and for each value, add the matching key(s) (e for exact match, R to get the reverse list based on value, not key) and add that to the ordered_keys array.

Post Deleted by Stéphane Chazelas
added 262 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

A shell is before all a tool to run other tooltools. It sounds to me you're after a programming language like perl, ruby, python...

Having said that, here is some possible solution for zsh.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do things like:

typeset -A A B
A=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)

for v ("${(@nO)A}") B+=("${(@kv)A[(eR)$v]}")

That is, order (O) the list of values ($A) numerically (n) and for each value, add the key/value pairs matching the value $v (e for exact match, R to get the reverse list based on value, not key) and add that to the B associative array.

Then, you'd get the sorted list in B:

$ printf '%s => %s\n' "${(@kv)B}"
192.168.8.1 => 9
192.168.1.1 => 9
192.168.2.2 => 5
192.168.3.2 => 1

And you can select the first 2 keys with

$ print -rl -- ${${(k)B}[1,2]}
192.168.8.1
192.168.1.1

A shell is before all a tool to run other tool. It sounds to me you're after a programming language like perl, ruby, python...

Having said that, here is some possible solution for zsh.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do things like:

typeset -A A B
A=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)

for v ("${(@nO)A}") B+=("${(@kv)A[(eR)$v]}")

Then, you'd get the sorted list in B:

$ printf '%s => %s\n' "${(@kv)B}"
192.168.8.1 => 9
192.168.1.1 => 9
192.168.2.2 => 5
192.168.3.2 => 1

And you can select the first 2 keys with

$ print -rl -- ${${(k)B}[1,2]}
192.168.8.1
192.168.1.1

A shell is before all a tool to run other tools. It sounds to me you're after a programming language like perl, ruby, python...

Having said that, here is some possible solution for zsh.

In zsh, you can get a sorted list of the keys of an associated array (${(kOn)A}) or of the values (${(On)A}) but not directly a list of keys from the sorted list of values (AFAIK), but you could do things like:

typeset -A A B
A=(
  192.168.2.2 5
  192.168.3.2 1
  192.168.1.1 9
  192.168.8.1 9
)

for v ("${(@nO)A}") B+=("${(@kv)A[(eR)$v]}")

That is, order (O) the list of values ($A) numerically (n) and for each value, add the key/value pairs matching the value $v (e for exact match, R to get the reverse list based on value, not key) and add that to the B associative array.

Then, you'd get the sorted list in B:

$ printf '%s => %s\n' "${(@kv)B}"
192.168.8.1 => 9
192.168.1.1 => 9
192.168.2.2 => 5
192.168.3.2 => 1

And you can select the first 2 keys with

$ print -rl -- ${${(k)B}[1,2]}
192.168.8.1
192.168.1.1
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k
Loading