Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Empty element elision and associative arrays (was Re: Slurping a file)




On 2024-01-19 08:01, Mikael Magnusson wrote:
Keys are unique but values aren't, so it's sort of a nonsensical
request; that said, you can do it ;).

I dunno, you might have some sort of efficiency test and store the results in an array keyed to the names of the various tests and you want to see them in the order of best to worst.

I think that for the time being this works for me:


typeset -A array=( [1test_one]=123 [2test_two]=345 [3test_three]=111 [4test_four]=5 )

echo "\nraw"
printf "\n%-20s %s" ${(kv)array}
echo "\n\nsorted on key"
printf "\n%-20s %s" ${(kv)array} | sort
echo "\nsorted on value"
printf "\n%-20s %s" ${(kv)array} | sort -k2
echo "\nsorted on value numerically"
printf "\n%-20s %s" ${(kv)array} | sort -k2g

output:


5 /aWorking/Zsh/Source/Wk 0 % . test2

raw # No recognizable order

1test_one            123
2test_two            345
4test_four           5
3test_three          111

sorted on key # If the array itself won't do it, then make 'sort' do it:

1test_one            123
2test_two            345
3test_three          111
4test_four           5

sorted on value # dictionary sort

3test_three          111
1test_one            123
2test_two            345
4test_four           5

sorted on value numerically # numeric sort

4test_four           5    # This isn't going anywhere.
3test_three          111
1test_one            123
2test_two            345  # Here's the winner, congratulations test_two team.

... so however much I think there should be a defined order to the way the arrays print, it seems easy enough to use other tools to do it.




Messages sorted by: Reverse Date, Date, Thread, Author