OK, So I want to identify a value in MSA 1 is in a list of Strings. I have
HL7.{MSA:1} In ("AE", "AR", "CE", "CR")
That doesn't work, so what should it be?
Figured this out. it's
HL7.{MSA:1} In ("AE,AR,CE,CR")
Related
There are 10 nodes in the network and I wanted to assign Node [0] to Node [4] with ValueA= 10, the rest with Value=90 in OMNETPP.INI. The only silly way I could think of to assign the value as follows:
**.*Node[0].ValueA= "10"
**.*Node[1].ValueA= "10"
......
......
**.*Node[5].ValueA= "90"
**.*Node[6].ValueA= "90"
.......
.......
I'm thinking to assign the value with a more efficient way with FOR loop but I don't think it is possible in the OMNETPP.INI.
Can anyone help to enlighten me how to achieve this? thank you.
Various options and ways to do this are available.
You could for example use:
**.Node[0..4].ValueA = "10"
**.Node[5..9].ValueA = "90"
Check the OMNET Simulation manual -> Chapter Wildcard patterns for additional information.
I need to extract values from an array of hashes:
data =
[{:diaria_media=>"103.58908136482939632545931759",
:room_night=>"1143",
:valor_periodo=>"118402.320000"},
{:diaria_media=>"307.46792079207920792079207921",
:room_night=>"101",
:valor_periodo=>"31054.260000"},
{:diaria_media=>"313.000000",
:room_night=>"9",
:valor_periodo=>"2817.000000"},
{:diaria_media=>"0.0",
:room_night=>"7",
:valor_periodo=>"0.0"},
{:diaria_media=>"4.4630434782608695652173913043",
:room_night=>"414",
:valor_periodo=>"1847.700000"},
{:diaria_media=>"150.89382627422828427853553482",
:room_night=>"1393",
:valor_periodo=>"210195.100000"},
{:diaria_media=>"221.11425992779783393501805054",
:room_night=>"554",
:valor_periodo=>"122497.300000"},
{:diaria_media=>"36.919200",
:room_night=>"25",
:valor_periodo=>"922.980000"},
{:diaria_media=>"31.967530864197530864197530864",
:room_night=>"81",
:valor_periodo=>"2589.370000"},
{:diaria_media=>"0",
:room_night=>"0",
:valor_periodo=>"0.000000"}]
I need to get all the :room night fields and add the values. What is the best way to achieve that?
first: it's not nice to ask for help and to format the question as you did.
second: the question has nothing to do with Rails or with Savon.
This is a pure Ruby question.
The solution seems simple to me.
You iterate over your array and summarize the numbers for each key :room_night
For example like this:
nights = 0
data.each do |booking|
nights += booking[:room_night].to_i
end
print "nights=#{nights}\n"
If you go functional it's even more simple:
nights = data.map{|e| e[:room_night].to_i}.reduce(:+)
and done!
As a bonus I put a executable script into Pastbin https://pastebin.com/29nMTYrK
I'm trying to build an array of values that come from an array of hashes, at the moment my code looks like this:
ids = array_of_hashes.inject([]) do |result,instance|
result << instance[:id]
result
end
I just want to know if there's a more efficient way to do it?
You could change it to look like:
ids = hash.map { |instance| instance[:id] }
Not necessarily more efficient, but easier to read and maintain!
Good luck!
There are two easy ways for it:
1. ids = hash.collect{|h| h[:id]}
2. ids = hash.map{|h| h[:id]}
Now you would ask what is the difference in both? for explanation see this accepted answer
I am creating a program that sorts a list (Just like list.sort()) but it won't count vowels. Here is what I mean:
I have a list of... ['acd', 'ibc', 'ebb', 'zzaeib'] or something similar. A normal sort program would give this as the result: ['acd', 'ebb', 'ibc', 'zzaeib']. My sort will need to disregard the vowels, sort it, and the put the vowels back in and return the resulting list. For example, it would see the list above as ['cd', 'bc', 'bb', 'zzb']. It would then sort it (['bb', 'bc', 'cd', 'zzb']) and put the vowels back in (['ebb', 'ibc', 'acd', 'zzaeib']).
Here are the differences:
Normal sort: ['acd', 'ebb', 'ibc', 'zzaeib']
Custom sort: ['ebb', 'ibc', 'acd', 'zzaeib']
I know I can use the key feature of sort (list.sort(key=whatever_key)), but I cannot see a way to do this. Thanks in advance.
Rob.
I know I can use the key feature of sort
Yep, you were almost there.
import re
new_list = sorted(l, key=lambda s: re.sub('[aioue]', '', s))
Given a json array:
[{ "x":"5", "y":"20" },{ "x":"6", "y":"10" },{ "x":"50", "y":"5" }]
I'd like to find argmax(x), such that I can do puts argmax(arr, :arg => "x").y and get 5. How can I elegantly implement this in Ruby?
Edit: Clarified a bit. The idea is that you can specify the field of an element in a list that you want to maximize and the method will return the maximizing element.
I think you want Enumerable#max_by. To get y like you're saying, it would be:
arr.max_by {|hash| hash['x']}['y']
(Well, actually, you'll want the numbers to be numbers instead of strings, since '50' sorts lower than '6'. But I think you get the idea. You can to_i or do whatever processing you need in the block to get the "real" value to sort by.)