I am trying to insert information from a hash monthly_multipliers into an iterator.
month_multipliers = { april: 1, may: 2, june: 3, july: 4, august: 5, september: 6, october: 7, november: 8, december: 9, january: 10, february: 11, march: 12}
months = ['april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'january', 'february', 'march']
I then iterate through each month, and need to inject the monthly multiplier for each month, so june would be 3 august 5 etc.
months.each do |month|
instance_variable_set :"##{month}_income", (monthly_new_account_income * month_multipliers[month]) + previous_years_monthly_income
end
This bit doesn't work
month_multipliers[month])
I tried sending and getting an instance variable, but cannot get the format right.
month_multipliers has keys in Symbol objects, but months has keys as String objects. Use either all Symbol or String objects or convert keys to the correct object before retrieving the values from the hash.
This worked
#month_multipliers[month.to_sym]
I see your issue, youre using symbolic keys with string values. you can directly iterate over the month_multiplier for simplicity
month_multipliers.each_pair do |month, multiplier|
instance_variable_set :"##{month}_income", (monthly_new_account_income * multiplier) + previous_years_monthly_income
end
Related
I have Year number and Month Number in my data.
How using DAX can I get the month name out of month number?
In SSRS its very easy. But how to achieve that using DAX?
You can use:
MonthName = FORMAT(DATE(1, [Num], 1), "MMM")
Result:
Nothing fancy, simply a reconstruction of a fake date from the month number provided, and reformat it with the FORMAT function.
Of course as an alternative you can go the old-fashioned way and write a SWITCH statement and hard-coded for the 12 months. It's up to you.
You can try this too:
Month name = FORMAT('Table'[date_column], "MMM")
If you use single quotes in 'MMM', it doesn't work. Ensure to use ""
By Use of Switch DAX function
MonthName = switch(True(),
MonthID = 1, "jan",MonthID = 2, "Feb",MonthID = 3, "March",MonthID = 4, "April",MonthID = 5, "May",MonthID = 6, "June",MonthID = 7, "july",MonthID = 8, "Aug",MonthID = 9, "Sept",MonthID = 10, "Oct",MonthID = 11, "Nov",MonthID = 12, "Dec"
)
In Laravel is it possible to select only one field and return that as a set/ array.
For example consider the model Foo which is linked to table foos which has field id, a, b, c.
Consider the following sample data:
(1, 10, 15, 20)
(1, 12, 15, 27)
(1, 17, 15, 27)
(1, 25, 16, 29)
(1, 28, 16, 40)
Now if I wanted to create a query that returns all the values of a where b is 15, I could do that like so:
Foo::select('a')->where('b', 15)->get();
However this will return an eloquent collection.
Instead how can I return instead an array like this:
[10, 12, 17]
Just use pluck() and ->toArray():
Foo::where('b', 15)->pluck('a')->toArray();
Laravel's pluck() method.
Laravel's toArray() method.
Do
Foo::where('b', 15)->lists('a')->all();
This will give you an array of ids. eg [2, 3, 5]
The lists method is deprecated and pluck need some parameter so, if you want to get all the attributed in array format, use it this way.
Foo::select('a')->where('b', 15)->get()->all();
I have a list whose keys is the date and the value is the hour of that day a sample looks like this
dtlist = [{'26/12/2010','01'},{'27/3/2008','00'},{'7/7/2007','10'},{'7/7/2007','23'}]
The output that I should get is
dtlist= [{'7/7/2007','10'},{'7/7/2007','23'},{'27/3/2008','00'},{'26/12/2010','01'}]
I want the date to be sorted by year, month, day and when the date is the same sort by hour in increasing order. Many thanks.
You have a list of unordered set litterals. I'm assuming you actually want to use something else, like a list of lists, or a list of tuples.
Here's how you can do it with a list of lists, or a list of tuples:
a = [['26/12/2010','01'],['27/3/2008','00'],['7/7/2007','10'],['7/7/2007','23']]
def k(a):
d = list(map(int, a[0].split("/")))
return d[::-1] + [int(a[1])]
print(sorted(a, key=k))
outputs:
[['7/7/2007', '10'], ['7/7/2007', '23'], ['27/3/2008', '00'], ['26/12/2010', '01']]
The key function returns the key for an item in the list, which is then used by the sorting function to sort the list.
In this example your input data corresponds to these keys:
items: [['7/7/2007', '10'], ['7/7/2007', '23'], ['27/3/2008', '00'], ['26/12/2010', '01']]
keys: [[2007, 7, 7, 10], [2007, 7, 7, 23], [2008, 3, 27, 0], [2010, 12, 26, 1]]
We thus sort by year, followed by month, followed by day, and lastly hour.
I have a hash set in my code that declares an elegibility scale:
eligibility_scale = [{family_size: 2, minimum_income: 20161, maximum_income: 38200},
{family_size: 3, minimum_income: 25393, maximum_income: 43000},
{family_size: 4, minimum_income: 30613, maximum_income: 47750},
{family_size: 5, minimum_income: 35845, maximum_income: 51600},
{family_size: 6, minimum_income: 41065, maximum_income: 55400},
{family_size: 7, minimum_income: 46297, maximum_income: 59250},
{family_size: 8, minimum_income: 51517, maximum_income: 63050}]
I'm trying to get the hash result using the family_size as the search parameter.
income_bracket = eligibility_scale.select{|h| h["family_size"] == 4 }
# Expecting: {family_size: 4, minimum_income: 30613, maximum_income: 47750}
After running this code I'm getting an empty array.
Any suggestions on what I'm doing wrong?
Hash keys in the question are symbols, not strings.
You need to specify the key as symbol:
Replace following:
h["family_size"]
with:
h[:family_size]
Using select will return an array, first of all, so if your family_size is unique, you can just use find. Otherwise, use select in the same way, but expect to have an array of hashes returned.
Second of all, your hashes are using symbols, not string. Ideally, you're looking at writing something like this:
eligibility_scale.find { |i| i[:family_size] == 4 }
# => {family_size: 4, minimum_income: 30613, maximum_income: 47750}
This is assuming your family size is unique.
You've defined your hash using the key: value syntax which is shorthand for :key => value - meaning the keys are symbols and not strings
eligibility_scale.select {|h| h[:family_size] == 4 }
I have two models Project and UrlList. A project :has_many url_list and a url_list :belongs_to project.
Now I have array for project id's all_projects = [1,2,5,8,16]. I want to retrieve all the records from url_list where project_id is one of those from all_projects array. How do I write code for it?
You can pass an array as value for an attribute to where method:
all_projects = [1, 2, 5, 8, 16]
url_lists = UrlList.where(:project_id => all_projects)
It'll generate SQL query like that:
SELECT `url_lists`.* FROM `url_lists` WHERE `project_id`.`user_id` IN (1, 2, 5, 8, 16)