This question already has an answer here:
How to put all regex matches into a string list
(1 answer)
Closed 2 years ago.
The following regex allows for extraction of all dates (XXXX-XX-XX XX:XX) from a particular key (Key1).
RegExp regExp = new RegExp(
r'(?<=Text\("key1:\[[^\][]*?)\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(?=[^\][]*])',
);
var match = regExp.allMatches(decrypted).map((z) => z.group(0));
prefs.setStringList("mykey",match);
With this code I have in output from match
(2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, ..., 2020-09-03 10:00, 2020-09-03 10:02)
The problem is prefs.setStringList is a List and accept only this format ["","","",""]. How can I adapt my output to be compatible?
allMatches returns an Iterable, which is not the List that SharedPreferences requires. Use the toList method to pass the correct data type.
prefs.setStringList("mykey",match.toList());
Related
This question already has an answer here:
Ruby Array Initialization [duplicate]
(1 answer)
Closed 3 years ago.
Recently I was playing with arrays and found out a weird behavior. I created a new array using Array.new.
arr = Array.new(4,"happy")
["happy", "happy", "happy", "happy"]
I appended a word into the second element in that array like shown below
arr[1] <<" Dino"
When I looked at arr, I was expecting an array with the second element having an appended word. But to my surprise array returned with all elements with the appended word.
["happy Dino", "happy Dino", "happy Dino", "happy Dino"]
How can this happen? Are we creating copies of the same string when we are creating the array? It doesn't happen if we use arr[1]= " Dino". Somebody can explain why this is happening?
Yes, you a right. See the ruby docs
When a size and an optional default are sent, an array is created with size copies of default. Take notice that all elements will reference the same object default.
You can initialize array in a way like this:
arr = Array.new(4){ 'happy' }
When we take Array.new(4, "Happy") it will create 4 elements with same object id. We can observe in irb arr[1].object_id => 2880099 , arr[2].object_id => 2880099 and remaining 2 elements also will return same object id.
So when we try like arr[1] << "Something" , this string will append to all the same object id's.
Now if we take another element for arr like arr.push("newString"). Now if see the last element object id arr.last.object_id => 4889988,it's different now.
So if we try same command arr[1] << "Something" it won't be effect to newly inserted element because object id is different.
if you don't want this behaviour the use Array.new(4){ 'String' }
I have a filter that I don't want to use at the launch of the application, only on a certain action. I know there is already a question about this but it doesn't help me, I actually don't understand both of the answers.
I was in a logic of "my column = value or 1 = 1" to get all my datasets instead of just the filter if it's not called.
Here's what I wrote :
FILTER (([ct]='%ct%') or '%ct%' = '%ct%')
VALIDATION
'ct' '^[a-zA-Z\-]+$'
END
I call my layer with a param on Openlayers 3 with
url: 'http://localhost:5000/cgi-bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/essai.map&SERVICE=WMS&VERSION=1.1.1%20&REQUEST=GetCapabilities',
serverType: 'mapserver',
params: {'LAYERS': 'aisdata', 'ct':'myvalue', 'TILED': true}
});
But all my dataset is returned. (If I remove '%ct%' = '%ct%' in my mapfile, the filter is well applied)
Can anyone help me to ignore my condition please?
Add a default value in the VALIDATION block, so that your value defaults to a empty string, and than add a OR condition in the FILER block which check if the value is a empty string:
VALIDATION
'ct' '^[a-zA-Z\-]+$'
'default_ct' '' # <-- ct will be a empty string if not provided via URL
END
FILTER (([ct]='%ct%') or ('%ct%' = '') )
If the database column ct does have a numeric type, the previous filter will produce a internal server error, because you cannot compare a empty string with a number. In this case use a numeric value as default, something like 0 or -1.
This question already has answers here:
How to perform sql "LIKE" operation on firebase?
(5 answers)
Closed 6 years ago.
Firebase queryEqualToValue returns items EQUAL to the specified key or value. How can we query for items PARTIALLY MATCH to the specified key or value?
Firebase iOS retrieve data documentation
We can combine startAt() and endAt() to limit both ends of our query.
The following example finds all dinosaurs whose name starts with the
letter "b":
let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
ref.queryOrderedByKey().queryStartingAtValue("b").queryEndingAtValue("b\u{f8ff}")
.observeEventType(.ChildAdded, withBlock: { snapshot in
println(snapshot.key)
})
The f8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with a b.
Retrieving Data
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Trying to add the contents of an array together.
["75.00", "50.00", "25.00"] the way I'm getting that info is with
c = Credit.last
c.payments.map(&:payment_amount).to_a
I'm trying to add up all the values together in the array.
The other posters are correct in that your question doesn't conform to the how to ask guidelines. The responses are not intended to put you down but rather to maintain the quality of content for stack overflow. That said this should get you where you need to go. IN the future please read the guidelines and submit accordingly.
Credit.last.payments.sum(:payment_amount.to_f)
One thing you may not have considered is that the array ["75.00", "50.00", "25.00"] contains a bunch of strings.
If you were to sum them together like this:
["75.00", "50.00", "25.00"].sum
# or like this as one commenter suggested
["75.00", "50.00", "25.00"].reduce(&:+)
# or the long-handed version
["75.00", "50.00", "25.00"].reduce {|str, val| str + val }
You would actually get "75.0050.0025.00". This is because the individual strings in the array are getting concatenated together.
So in fact, you would need to convert the array to floats or integers first. This can be done like this:
floats = ["75.00", "50.00", "25.00"].collect(&:to_f)
# or the long-handed version
["75.00", "50.00", "25.00"].collect {|val| val.to_f }
Then you can sum the values:
sum = floats.sum
Edit:
I just tried summing a string column via ActiveRecord and got an exception ActiveRecord::StatementInvalid: TinyTds::Error: Operand data type nvarchar is invalid for sum operator.:.
payment_total = Credit.last.payments.sum(:payment_amount)
# returns ActiveRecord::StatementInvalid:
# TinyTds::Error: Operand data type nvarchar is invalid for sum
# operator.
Looks like that won't be an option for you. Although, you could change the datatype of the column so that it is something other than a string. If you change the column datatype then you will be able to use aggregate functions.
This question already has answers here:
Ruby ampersand colon shortcut [duplicate]
(2 answers)
What does map(&:name) mean in Ruby?
(17 answers)
Closed 9 years ago.
Here's the line of code I'm trying to wrap my head around:
Category.all.map(&:id).each { |id| Category.reset_counters(id, :products) }
Hoping someone can help me understand what (&:id) is doing and how it impacts the rest of the line? I believe it turns the symbol :id into a proc that'll respond to id?!? But then it gets confusing...
Thanks in advance!
Category.all.map(&:id)
is shorthand for
Category.all.map { |a| a.id }
as for how it affects the rest of the line, the above section returns all id values as a single Array. This Array of ids is then passed into another call to each, which iteratively passes each id into reset_counters.