Skip to content

Instantly share code, notes, and snippets.

@hkoba
Last active August 1, 2022 06:01
Show Gist options
  • Save hkoba/9e5f9bdb7002eb34230ef68be343db97 to your computer and use it in GitHub Desktop.
Save hkoba/9e5f9bdb7002eb34230ef68be343db97 to your computer and use it in GitHub Desktop.
zsh functions to read/print LTSV (Labeled Tab-separated Values)
#
# Usage: read_ltsv_as_assoc VARNAME < LTSV_STREAM
#
function read_ltsv_as_assoc {
if ((ARGC != 1)); then
echo 1>&2 "Invalid number of Argument(s)"; return 1
fi
local line vn=$1; shift;
IFS=$'\t' read -A line || return 1;
local i kv tmp; tmp=()
for i in $line; do
kv=("${(@s/:/)i}")
tmp+=("$kv[1]" "${(j/:/)kv[2,$#kv]}")
done
if (($+parameters[$vn])); then
unset $vn
fi
typeset -gA $vn;
set -A $vn "$tmp[@]"
}
#
# Usage: print_pairs_as_ltsv key value key2 value2 ...
#
function print_pairs_as_ltsv {
local k v tmp
tmp=()
for k v in "$@"; do
tmp+=("$k:$v")
done
print ${(j/\t/)tmp}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment