How to iterate through Blackberry Resource fie? - blackberry

How to iterate the keys in a Resource fie (.rrc) in a loop & how do I get the length or the no. of elements in the resource file, I mean the end of the file ?
I want to iterate through Resource file as at Run-time I m getting a String which I want to compare with all the Resource KEYs & if the String which I got matches with any of the KEY then its corresponding VALUE is fetched using ,
currentValue = _resources.getString(KEY);
So I m trying to iterate through it using
for(int i=0; i< (get Resource total Length); i++)
{
key = (get Resource KEY)
if(string.equals(key))
currentValue = _resources.getString(key);
}
but I m not getting "how to get Resource total length (no of elements in Resource file) & its KEY".
any solution for these issues ?

Since it isn't an order list of resources on the implementation side, there really isn't a way to do exactly what you are asking. However, you could put all your elements into an array within the resource and then easily work with those resources in the code like you would work with any other array. Here's the syntax for declaring an array within a resource file:
RESOURCE_ARRAY#0={
"HEY THERE",
"WHAZZUP!",
"NICE ARRAY OF STRINGS YOU GOT THERE",
"THANKS, I WORKOUT",
"IT SHOWS!",
};

I'm not sure why you would want to iterate through resources. You may, however, define arrays as resources, then iterate through the array which is pulled as a single resource.

Related

How do I iterate over a list of strings in dxl?

I have a list of strings that I want to iterate over in dxl. They represent module ID's like so:
string limitModules[5] = ['1', '2', '3', '4', '5']
Obviously each of the module ID's are more complicated. I've constructed a for loop using the syntax of for type1 v1 in type2 v2 do. However when I run the script I get the following error:
incorrect arguments for (do)
Here's my loop exactly:
string mod_name = ""
for mod_name in limitModules do {
// test to see if module is found
}
What am I missing?
You can create a Skip List to put them in and iterate over it that way (see skip lists in the DOORS DXL Help).
Or if you need to use the String array that way you can use the following:
for(i = 0; i < 5; i++)
{
mod_name = limitModules[i]
// other code here
}
This method is fine if you know the size of the array. However skip lists are more efficient for this sort of thing.
You can do that. A Skip List is not necessary. I am also using the approach for limiting modules before traversing the links.
But you have to change the declaration:
string limitModulesLevel1[] = {"Some Name", "Even some other"}
for(i=0; i<sizeof(limitModulesLevel1); i++)
{
...
}

Convert a Dart Row Object to a Map. (sqlJocky)

I'd like to get the row data given to me by sqlJocky in my Dart Server application and convert it (with column names) to a Map. Ie. row['email'] == "somename".
The row data is a "_BinaryDataPacket" which is an extension of Row type. Right now the method recommended by the sqlJocky developer to access the data involves either knowing the column name of what your accessing in the database:
row.email == "somename"
or just ignoring the column name all together:
row[0] == "somename"
I've tried a few hacks to get at the column data and even edited the original sqlJockey code to get at _BinaryDataPacket._fieldIndex to made it public. While that did give me access to Symbol instances of the Column titles to build a map with, I would like to avoid modifying the developers stable code if at all possible.
I assume there has to be an easy way to get the column names and put them in a Map with the row data.
TLDR:
I want to Convert alpha.brava == "charle" into alpha["bravo"] == "charle".
Thanks
In your results, you have a fields list attribute.
So you could do this :
.then((Results results) {
results.forEach((Row row) {
Map r = new Map();
for(int i=0; i<results.fields.length; i++) {
r[results.fields[i].name] = row[i];
}
});
});
This will map the data from a row to the map

How to include duplicate records in query?

Given an array of part ids containing duplicates, how can I find the corresponding records in my Part model, including the duplicates?
An example array of part ids would be ["1B", "4", "3421", "4"]. If we assume I have a record corresponding to each of those values I would like to see 4 records returned in total, not 3. If possible, I was hoping to be able to make additional SQL operations on whatever is returned.
Here's what I'm currently using which doesn't include the duplicates:
#parts = Part.where(:part_id => params[:ids])
To give a little background, I'm trying to upload an XML file containing a list of parts used in some item. My application is meant to parse the XML file and compare the parts listed within against my Parts database so that I can see how much the part weighs. These items will sometimes contain duplicates of various parts so that's what I'm trying to account for here.
The only way I can think of doing it is using map...
#parts = params[:ids].map { |id| Part.find_by_id(id) }
hard to tell exactly what you are doing, are you looking up weight from the xml or from your data?
parts_xml = some_method_that_loads_xml
part_ids_from_xml = part_xml.... # pull out the ids
parts = Part.where("id IN (?)", part_ids_from_xml)
now you have two arrays (xml data and your 'matching' database records) and you can use select or detect to do in memory lookups by part_id
part_ids_from_xml.each do |part_id|
weight = parts.detect { |item| item.id == part_id }.weight
puts "#{id} weighs #{weight}"
end
see http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-detect
and http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-select

how to sort documents using the erlang map reduce in riak

i'm using riak to store json documents right now, and i want to sort them based on some attribute, let's say there's a key, i.e
{
"someAttribute": "whatever",
"order": 1
}
so i want to sort the documents based on the "order".
I am currently retrieving the documents in riak with the erlang interface. i can retrieve the document back as a string, but i dont' really know what to do after that. i'm thinking the map function just reduces the json document itself, and in the reduce function, i'd make a check to see whether the item i'm looking at has a higher "order" than the head of the rest of the list, and if so append to beginning, and then return a lists:reverse.
despite my ideas above i've had zero results after almost an entire day, i'm so confused with the erlang interface in riak. can someone provide insight on how to write this map/reduce function, or just how to parse the json document?
As far as I know, You do not have access to Input list in Map. You emit from Map a document as 1 element list.
Inputs (all the docs to handle as {Bucket, Key}) -> Map (handle single doc) -> Reduce (whole list emitted from Map).
Maps are executed per each doc on many nodes whereas Reduce is done once on so called coordinator node (the one where query was called).
Solution:
Define Inputs (as a list or bucket)
Retrieve Value in Map and emit whole doc or {Id, Val_to_sort_by)
Sort in Reduce (using regular list:keysort)
This is not a map reduce solution but you should check out Riak Search.
so i "solved" the problem using javascript, still can't do it using erlang.
here is my query
{"inputs":"test",
"query":[{"map":{"language":"javascript",
"source":"function(value, keyData, arg){ var data = Riak.mapValuesJson(value)[0]; var obj = {}; obj[data.order] = data; return [ obj ];}"}},
{"reduce":{"language":"javascript",
"source":"function(values, arg){ return [ values.reduce(function(acc, item){ for(var order in item){ acc[order] = item[order]; } return acc; }) ];}",
"keep":true}}
]
}
so in the map phase, all i do is create a new array, obj, with the key as the order, and the value as the data itself. so visually, the obj is like this
{"1":{"firstName":"John","order":1}
in the reduce phase, i'm just putting it in the accumulator, so basically that's the sort if you think about it, because when you're done, everything will be put in order for you. so i put 2 json documents for testing, one is above, the ohter is just firstName: Billie, order 2. and here is my result for the query above
[{"1":{"firstName":"John","order":1},"2":{"firstName":"Billie","order":2}}]
so it works! . but i still need to do this in ERLANG, any insights?

How do I collect and combine multiple arrays for calculation?

I am collecting the values for a specific column from a named_scope as follows:
a = survey_job.survey_responses.collect(&:base_pay)
This gives me a numeric array for example (1,2,3,4,5). I can then pass this array into various functions I have created to retrieve the mean, median, standard deviation of the number set. This all works fine however I now need to start combining multiple columns of data to carry out the same types of calculation.
I need to collect the details of perhaps three fields as follows:
survey_job.survey_responses.collect(&:base_pay)
survey_job.survey_responses.collect(&:bonus_pay)
survey_job.survey_responses.collect(&:overtime_pay)
This will give me 3 arrays. I then need to combine these into a single array by adding each of the matching values together - i.e. add the first result from each array, the second result from each array and so on so I have an array of the totals.
How do I create a method which will collect all of this data together and how do I call it from the view template?
Really appreciate any help on this one...
Thanks
Simon
s = survey_job.survey_responses
pay = s.collect(&:base_pay).zip(s.collect(&:bonus_pay), s.collect(&:overtime_pay))
pay.map{|i| i.compact.inject(&:+) }
Do that, but with meaningful variable names and I think it will work.
Define a normal method in app/helpers/_helper.rb and it will work in the view
Edit: now it works if they contain nil or are of different sizes (as long as the longest array is the one on which zip is called.
Here's a method that will combine an arbitrary number of arrays by taking the sum at each index. It'll allow each array to be of different length, too.
def combine(*arrays)
# Get the length of the largest array, that'll be the number of iterations needed
maxlen = arrays.map(&:length).max
out = []
maxlen.times do |i|
# Push the sum of all array elements at a given index to the result array
out.push( arrays.map{|a| a[i]}.inject(0) { |memo, value| memo += value.to_i } )
end
out
end
Then, in the controller, you could do
base_pay = survey_job.survey_responses.collect(&:base_pay)
bonus_pay = survey_job.survey_responses.collect(&:bonus_pay)
overtime_pay = survey_job.survey_responses.collect(&:overtime_pay)
#total_pay = combine(base_pay, bonus_pay, overtime_pay)
And then refer to #total_pay as needed in your view.

Resources