Because the data I am working with, the dictionary key "Courses" may contain (1) an array of [multiple] dictionary value(s), or (2) one dictionary value, in other words, not an array. This means at a few points in my code I need to go about accessing the data like the following:
// earlier in a different function multipleCourses is determined, depending upon // whether the data contains more than one course function getCourseValue (courseIndex) { // courseIndex is known if there are multiple Courses, but I need to write my functions // this way to handle both possible cases, i.e., if only one course or more than one if (multipleCourses) { System.println( loadedData["data"]["courses"][CourseIndex]["name"].toString() ); } else { System.println( loadedData["data"]["courses"]["name"].toString() ); } }
As you can see, this is clunky. I would like to simplify things, save memory and make things quicker. Is it possible to declare a variable/pointer to refer/point to a sub-dictionary, so I no longer need to differentiate between the single and multiple cases? For instance, lets say after multipleCourses is determined, and I know I am no longer concerned about the other courses, but now all I want to focus on is on course 3... could I define a variable like the following, and save having to use the courseIndex or the multipleCourses variable throughout the rest of the program?
var chosenCourse = loadedData["data"]["courses"][3]; // since at a point in my program the user has chosen one course, so // we no longer care if there are multiple courses, or even about the // other courses. So I declare this shortcut variable to access the data easier // access Course Value in an "inline" manner, looks much cleaner, // and no longer has to use multipleCourses and indexCourse to access // the proper data... System.println( chosenCourse["Name"].toString() );
can anyone tell me if this is possible using Monkey C? thanks in advance for the feedback! ehh, I'm tired!!!