Convenient way to Retrieve Keys

11 views
Skip to first unread message

Luke

unread,
May 11, 2020, 12:43:17 PM5/11/20
to ticktick...@googlegroups.com
Hello. New here; hope this isn't asked every day.

Just started using TickTick for something I'm working on, and have run into the
following use case.

I need to retrieve not only values for an object/array, but also the keys. How
might I go about this?

For example:

``
test = {
"sequence": 12,
"method": "destructive",
"targets": [
"red",
"blue"
]
}
``

What if I wanted to know the keys for test, because, for example, it just came
back through curl and I didn't have them ahead of time?

# I can easily print values:
for val in ``test.items()``; do
echo "${!val}"
done
# OUTPUT:
#destructive
#12
#red
#blue

But there is no way I can find to print keys in a friendly-looking way.
Just as a wild example, I can't do this:

for item in ``test.items()``; do
echo ``$item.key()`` is "${!item}"
done
# OUTPUT (Array subscripts are shrunk from how TickTick pads them):
#type is destructive
#sequence is 12
#target[0] is red
#target[1] is blue

Or anything even vaguely like that.

I can probably find a way to brute force it, by writing a function that somehow
peals apart items() results by string lengths, but that seems very hacky and
non-TickTickish.

Thanks for any pointers.

Luke

Luke

unread,
May 11, 2020, 4:10:36 PM5/11/20
to ticktick...@googlegroups.com
To solve my immediate problem, I have devised a method that works, at least in
the very limited test cases I have run.

I would still like something better if it exists, or any methods using TickTick
internals, because I suspect this thing will crumble in a stiff breeze. I also
suspect it's slower than this task should be.
I thought I would post it here for anyone looking through the archives.

#Takes the array we want to split into, and the key we want split
function kSplitter {
arrayName="$1"
shift
local IFS='_'

for word in ${1:12}; do
eval $arrayName=\(\""$word"\"\)
done
}

# Test case for my "test" object (below)
for item in ``test.items()``; do
declare -a keys
kSplitter keys "$item"
# Print the most RHS key, and the value
echo "${keys[-1]}: ${!item}"
done

Luke

3.5 hours ago, I wrote:

> I need to retrieve not only values for an object/array, but also the keys.
> How might I go about this?
>
> For example:
>
> ``
> test = {
> "sequence": 12,
> "method": "destructive",
> "targets": [
> "red",
> "blue"
> ]
> }
> ``
>
> What if I wanted to know the keys for test, because, for example, it just
> came back through curl and I didn't have them ahead of time?
[.]
Reply all
Reply to author
Forward
0 new messages