kristopolous / TickTick

JSON in your Bash scripts
http://9ol.es/TheEmperorsNewClothes.html
Other
579 stars 55 forks source link

Iteration #15

Closed adamkijak closed 10 years ago

adamkijak commented 12 years ago
#!/bin/bash                                                                                                                                                                                    

. ticktick.sh                                                                                                                                                                                  

``                                                                                                                                                                                             
people = {                                                                                                                                                                                     
    [                                                                                                                                                                                          
        {                                                                                                                                                                                      
            "name":"Alice",                                                                                                                                                                    
            "age":23                                                                                                                                                                           
        },                                                                                                                                                                                     
        {                                                                                                                                                                                      
            "name":"Bob",                                                                                                                                                                      
            "age":43                                                                                                                                                                           
        },                                                                                                                                                                                     
        {                                                                                                                                                                                      
            "name":"Harry",                                                                                                                                                                    
            "age":13                                                                                                                                                                           
        }                                                                                                                                                                                      
    ]                                                                                                                                                                                          
}                                                                                                                                                                                              
``          

How can I iterate through this structures in the array? I mean want to do somthing like:

for person in ``peaople.items()``
  echo ``person.name``
  echo ``person.age``
done
kristopolous commented 12 years ago

I'm sorry, not trying to be mean, but that's not valid syntax. You are doing { [] } ... which is invalid, hashes have to have keys. I don't have elegant ways of dealing with syntax issues. Maybe we can figure something out.

kristopolous commented 12 years ago

As far as the original question ... even if you did the syntax fix we still have a problem ... because person gets expanded ... If you really want something, I think ${!person}_name will work, but that's not discoverable at all. Man, what a silly project this is ... I'll see what I can do about it. Thanks.

adamkijak commented 12 years ago

Of course you're right the syntax wasn't valid. I fixed it:

#!/bin/bash                                                                                                                                                                                    

. ticktick.sh                                                                                                                                                                                  

``                                                                                                                                                                                             
people = {                                                                                                                                                                                     
    "array" : [                                                                                                                                                                                
        {                                                                                                                                                                                      
            "name":"Alice",                                                                                                                                                                    
            "age":23                                                                                                                                                                           
        },                                                                                                                                                                                     
        {                                                                                                                                                                                      
            "name":"Bob",                                                                                                                                                                      
            "age":43                                                                                                                                                                           
        },                                                                                                                                                                                     
        {                                                                                                                                                                                      
            "name":"Harry",                                                                                                                                                                    
            "age":13                                                                                                                                                                           
        }                                                                                                                                                                                      
    ]                                                                                                                                                                                          
}                                                                                                                                                                                              
``                                                                                                                                                                                             

for person in ``people.array.items()``                                                                                                                                                         
do                                                                                                                                                                                             
    echo ${!person}                                                                                                                                                                            
done 

The main problem why it doesn't work is that each element of structure in array is treated as element of array. Output:

23
Alice
43
Bob
13
Harry

I don't call it big bug, just was curious what's TickTick is capable of. :) Maybe I'll think how to add this feature.

kristopolous commented 12 years ago

well this was a project to amuse my peers in the extreme shell scripting world ... not really to facilitate awesomeness. Let me satisfy a separate pull request and then address this.

kaos commented 11 years ago

I ran into this very same issue.. I have

{"id":"519f1f8c47c12c347900290a","name":"Test","desc":"","lists":[{"id":"519f1f8c47c12c347900290b","name":"To Do"},{"id":"519f1f8c47c12c347900290c","name":"Doing"},{"id":"519f1f8c47c12c347900290d","name":"Done"}]}

and want to do something like:

for list in ``lists.items()``; do
  echo "Item: ${!list}_name [${!list}_id]"
done

Will probably work around it with looping over the indexes instead based on lists.length()..