Skip to content

Instantly share code, notes, and snippets.

@przmv
Last active December 11, 2015 05:39
Show Gist options
  • Save przmv/4553978 to your computer and use it in GitHub Desktop.
Save przmv/4553978 to your computer and use it in GitHub Desktop.
Associative arrays implementation in rc shell script. Thanks to bnwr on #suckless for the idea ...and implementations
#!/usr/local/plan9/bin/rc
path=$PLAN9/bin
# List with odd items as keys and even items as values
data=('foo' 'bar' \
'bar' 'baz' \
'test' 'passed' \
'foo' 'one more bar' \
'dummy' 'qwerty' \
'quuix' 'dummy')
fn get {
if (! ~ $#* 0) {
k=1
v=0
while(test $k -le $#map) {
if (~ $1 $map($k)) {
v=`{echo $k + 1|bc}
}
k=`{echo $k + 2|bc}
}
if (! ~ $v 0)
echo $map($v)
}
}
map=$data get $1
#!/opt/plan9/bin/rc
path=$PLAN9/bin
# List with odd items as keys and even items as values
data=('foo' 'bar' \
'bar' 'baz' \
'test' 'passed' \
'foo' 'one more bar' \
'dummy' 'qwerty' \
'quuix' 'dummy')
fn get {
temp=$data
v=()
while (~ $#v 0 && ! ~ $#temp 0) {
if (~ $1 $temp(1)) {
v=$temp(2)
}
temp=$temp(3-)
}
if (! ~ $v 0)
echo $v
}
get $1
#!/opt/plan9/bin/rc
path=$PLAN9/bin
# List with odd items as keys and even items as values
data=('foo' 'bar' \
'bar' 'baz' \
'test' 'passed' \
'foo' 'one more bar' \
'dummy' 'qwerty' \
'quuix' 'dummy')
fn get {
if (! ~ $#* 1) {
if (~ $1 $*(2)) echo $*(3)
if not get $1 $*(4-)
}
}
get $1 $data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment