kristopolous / TickTick

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

Indexing With a Variable #17

Open abeam89 opened 12 years ago

abeam89 commented 12 years ago

Sorry to create a new issue if this should have been appended to an existing one instead, but I wanted it to be clear what I'm having an issue with.

I'm trying to iterate through a JSON array using TickTick and match a certain value. In this case, I'm accessing the RottenTomatoes API and doing a query. Since they have very basic query support, appending a year to the query doesn't help the results, and the first result from a query is usually wrong. They tend to provide results based on popularity rather than accuracy.

The value that I'm looking for in the search results is the year in order to improve the quality of my queries.

My thought was that I would iterate through the movie array, checking the year of each movie. Once I find a match, I can then use the index into the array for accessing other useful information about that particular movie. Unfortunately, I'm getting bad substitution errors the way I'm attempting to do this. From the output I'm seeing, it appears that the variable I'm trying to use as an index isn't getting expanded.

This is the error output that I'm getting when I try to use the variable $i as an index to the array 'movies' from this JSON data:

--: line 23: ${__tick_data_movies_$i_year}: bad substitution

Here is the way that I'm trying to make this happen:

for ((i=0;i<$PAGE_LIMIT;i++)); do
  if [ "``movies[$i]["year"]``" == "$MOVIE_YEAR" ]; then
    RESULT=$i
    break
  fi
done

I can confirm that the following, much like my initial example, results in a bad substitution error:

ZERO=0
FIRST_MOVIE_YEAR="``movies[$ZERO]["year"]``"

Conversely, I can confirm that the following works just fine:

FIRST_MOVIE_YEAR="``movies[0]["year"]``"

I tried implementing this using the pop() method, but I haven't gotten the results I expect with that method. This seems to me like it would be more effective for my purposes anyway, so I haven't pursued that too much further.

I also tried to use the items() method, but seems to be a tremendous waste for this purpose as well.

There may be a better way to do what I'm trying to do, and I'm fully open to suggestions for improvement. However, I do believe this is an issue with TickTick. I'm not sure if it was intended that this should work or not, but from what I read in the README I've drawn the conclusion that it should work just fine. Forgive me if I have made any error in drawing this conclusion.

Thank you very much for any help and/or feedback.

kristopolous commented 12 years ago

Thanks for using my library. Unfortunately, I can't really help you without looking at the actual implementation. Please send me the code and I'll run it and make sure it's working, hopefully mostly by patching my library. Try kristopolous (at) yahoo (dot) com for sending it.

I'll keep the code private and strictly confidential. Have a good one.

mliebler commented 11 years ago

My object is like this

 {"shows":[{"Name":"Luceaferul","id":"1","DayOfWeek":1}, {"Name":"Ghettoblaster","id":"2","DayOfWeek":1}, ...

i use the following function

function getmember {
    group=$1
    member=$2
    index=`printf "%012d" "$3"` 
    var="__tick_data_${group}_${index}_${member}";
    eval result=\$$var  
}

Will result in "Ghettoblaster"

getmember "shows" "Name" 2
echo $result

Thanks for great work

csebille commented 10 years ago

Thanks to @mliebler for the function. It allows me to keep going with ticktick. Thanks too to @kristopolous for this lib