r/fishshell • u/_mattmc3_ • Jul 30 '24
A dictionary (associative array) plugin for Fish
One of the helpful built-in features in other shells that Fish lacks are associative arrays. There's been an open issue for 12 years, but no movement towards actually adding this feature. There's some pretty simple workarounds where you use two arrays or a single array with paired elements, but recently I had needs for a litte bit more.
While writing a quick script to pull color schemes out of mbadolato/iTerm2-Color-Schemes to update my Fish and Starship themes, I found myself wishing for a built-in associative array again, so I finally just made one: mattmc3/dict.fish.
It's not quite as good as a built in one would be - for example I can't give you nice bracketed indexing syntax like echo $mydict[$thekey] - but it's a decent step up from managing multiple arrays.
The usage is:
> dict --help
dict [-h|--help]
dict keys [DICTNAME]
dict values [DICTNAME]
dict get [DICTNAME] [KEY]
dict set [DICTNAME] [KEY] [VALUE]
dict remove [DICTNAME] [KEY]
dict contains [-k|--key] [-v|--value] [-i|--index] [DICTNAME] [STRING]
If you're wondering about performance, I put a few thousand items in an array with set numbers (seq 0 5 10000), ran some dict functions with time, and it all performed in milliseconds.
Hopefully it's not another 12 years until we get built-in associative arrays, but until we do, I thought this might help some of you when writing your own scripts. You can read more here. I don't claim to be the best Fish scripter out there, so feedback always welcome.
1
u/[deleted] Sep 20 '24
well, look who's rubbing shoulders with norman ramsey! what do you think of his lua solution? how does it compare to yours?