(*it).second. How this keyword works? - stl

for(it=visited.begin(); it!=visited.end();++it)
{
if((*it).second>ttl){
++count;
}
}
What does the line if((*it).second>ttl) means here ?
For better understanding see this code please .... http://ideone.com/NY4ofJ .
Thanks in Advance .

A map is a collection of pair ; with a key and a value. To access to the key you use the member first and to access to the value you use member second.
(*it).second or it->second dereference the iterator and get the second value of the pair contains in the map.
So, the value contained by the iterator is tested with the value contained by the ttl variable.

The line in the question points to the second value of a Map or Pair ... for more :
See Here

Related

How to access the key of a jsoncpp Value

I kind of feel stupid for asking this, but haven't been able to find a way to get the key of a JSON value. I know how to retrieve the key if I have an iterator of the object. I also know of operator[].
In my case the key is not a known value, so can't use get(const char *key) or operator[]. Also can't find a getKey() method.
My JSON looks like this:
{Obj_Array: [{"122":{"Member_Array":["241", "642"]}}]}
For the piece of code to parse {"122":{"Member_Array":["241", "642"]}} I want to use get_key()-like function just to retrieve "122" but seems like I have to use an iterator which to me seems to be overkill.
I might have a fundamental lack of understanding of how jsoncpp is representing a JSON file.
First, what you have won't parse in JsonCPP. Keys must always be enclosed in double quotes:
{"Obj_Array": [{"122":{"Member_Array":["241", "642"]}}]}
Assuming that was just an oversight, if we add whitespace and tag the elements:
{
root-> "Obj_Array" : [
elem0-> {
key0-> "122":
val0-> {
key0.1-> "Member_Array" :
val0.1-> [
elem0.1.0-> "241",
elem0.1.1-> "642" ]
}
}
]
}
Assuming you have managed to read your data into a Json::Value (let's call it root), each of the tagged values can be accessed like this:
elem0 = root[0];
val0 = elem0["122"]
val0_1 = val0["Member_Array"];
elem0_1_0 = val0_1[0];
elem0_1_1 = val0_1[1];
You notice that this only retrieves values; the keys were known a priori. This is not unusual; the keys define the schema of the data; you have to know them to directly access the values.
In your question, you state that this is not an option, because the keys are not known. Applying semantic meaning to unknown keys could be challenging, but you already came to the answer. If you want to get the key values, then you do have to iterate over the elements of the enclosing Json::Value.
So, to get to key0, you need something like this (untested):
elem0_members = elem0.getMemberNames();
key0 = elem0_members[0];
This isn't production quality, by any means, but I hope it points in the right direction.

How to conditionally return specific value with another in JSONPath?

I've got an object like this one:
{
"content": {
"iteration_size": 1
}
}
I need a JSONPath which returns null if the value of next_item is exactly 1, or the value of iteration_size otherwise. So the above should return null and "iteration_size": 2 should return 2. Is this possible in JSONPath?
I've tried variations on $.content[?(#.iteration_size == 1)].iteration_size, but the two online evaluators I've tried even disagree on the results of several of my attempts.
The use case is the AWS Batch API, where the array size must be either null or a number between 2 and 10,000.
AWS Support suggested that this was not possible, and that I should instead use a branch in the step function. I ended up replacing this:
.next(batch_submit_job)
[…]
with this:
.next(
aws_stepfunctions.Choice(self, "check_maybe_array")
.when(
aws_stepfunctions.Condition.number_equals("$.content.iteration_size", 1),
single_batch_submit_job,
)
.otherwise(array_batch_submit_job)
.afterwards()
)
where only array_batch_submit_job has an array_size. Note how the rest of the step function ended up wrapped inside the last next() call rather than at the top level.

If a key has a value then print the value ruby

I'm trying to only put value of the key self only if the key escalation_policies has a value. This is an example a schedule that doesn't have an escalation policy hence I don't want the value for the key self
{"schedules"=>
[{"id"=>"0000000",
"type"=>"schedule",
"summary"=>"-PROD-",
"self"=>"https://api.pagerduty.com/schedules/",
"html_url"=>"https://pagerduty.com/schedules/",
"name"=>"-PROD-",
"time_zone"=>"America/",
"description"=>nil,
"privilege"=>nil,
"users"=>
[{"id"=>"0000000",
"type"=>"user_reference",
"summary"=>"DOo Kkkk",
"self"=>"https://api.pagerduty.com/users/",
"html_url"=>"https://target.pagerduty.com/users/"}],
"escalation_policies"=>[],
"teams"=>[]},
}
My code to conquer this challenge is:
somefile = File.open("Schedule_Api.txt", "a+")
jdoc.fetch("schedules").each do |schedule|
somefile.puts schedule["self"] if schedule["escalation_policies"].exists? == true
end
somefile.close
This variable jdoc is the curl for the website. And this is the result I get from this is undefined methodexists?' for []:Array (NoMethodError). Is there another alternative to this then the methodexists?`. Any help will be helpful! Thank you!
.exists? is a method from ActiveRecord, not on Array.
If you want to test for empty arrays, you probably want to do
somefile.puts schedule["self"] unless schedule["escalation_policies"].empty?
If you want to test for any value at all, just doing
somefile.puts schedule["self"] if schedule["escalation_policies"]
should work
Try:
if schedule.has_key?("escalation_policies")
You could also test:
if schedule["escalation_policies"]
which would return false not only if the key does not exist, but also if it does exist, and its corresponding value is nil.
If the absence of values will always be represented by an empty array ([]), then instead you can use schedule["escalation_policies"].empty? as mentioned by Rick Sullivan in his answer, or schedule["escalation_policies"].any? to test for an array size > 0.

How to filter the results of a hashMap to pick up all Keys starting with a certain letter Groovy

I have created a LinkedHashMap, which produces a list of statuses in groovy.
These are the results of a mysql query. I want to filter the results of my map to use key entries (statusName) in the map which start with the letter "O". However I am struggling to find how to do this with a map.
My code is as follows:
db.eachRow(reportQuery, [date]) {
cache.put(it.statusName, it.statusTickets)
}
cache.each{statusName, statusTickets ->
reportMetricsWithValues[statusName] = statusTickets
table.addData([metric:statusName, value: statusTickets])
}
This is the part of my code where I need to add this filter. The code is adding the key value metrics to a database table.
To filter, you would use findAll. Pass it the map element by element and check, if the key of the element starts with the letter O; Something along the lines of:
groovy:000> [zomg: 1, omg: 2].findAll{ it.key.startsWith('o' ) }
===> [omg:2]
If you also need the "others", then groupBy (same syntax like above) could prove useful.

Use variable as key in JSON data object to get value

I have a given JSON object .
var item = {VENUE_ID: "146", Cost: 0, Impressions: 0, Position: 0, id: 6} ;
I can extract value by item.VENUE_ID ,item.Cost.
How can i use variable to extract value .I want something like it
var keys="VENUE_ID";
item.keys or item[keys];
Solved it we can use following code to set key dynamic and get value item["" + keys+ ""]
For me
item[keys]
works perfectly where keys = "VENUE_ID"
Anyone facing this requirement, try and access the value the aforesaid way, even the OP posted the correct method in his question. I'm not sure why it didn't work for him in the first place.