Headings for 13 months on Array - arrays

I was wondering if someone can assist me, why the headers of the below code is not creating them correctly for 13 months. I would like to add Jan(LY) Feb(LY)......until Jan(TY).
Please see below Heading code only,
//Headings
J:=0;
for I:=12 downto 0 do
begin
J:=J+1;
Amonth:=IntToStr(J);
If basemonth-12 <=0 then Harray[J]:=Basemonth-I+13;
If basemonth-12 > 0 then Harray[J]:=Basemonth-I;
If Harray[J] >13 then Harray[J]:=Harray[J]-13;
//showmessage(INTTOSTR(Harray[J]));
end;
Heading1:=Monthcalc(Harray[1]);
Heading2:=Monthcalc(Harray[2]);
Heading3:=Monthcalc(Harray[3]);
Heading4:=Monthcalc(Harray[4]);
Heading5:=Monthcalc(Harray[5]);
Heading6:=Monthcalc(Harray[6]);
Heading7:=Monthcalc(Harray[7]);
Heading8:=Monthcalc(Harray[8]);
Heading9:=Monthcalc(Harray[9]);
Heading10:=Monthcalc(Harray[10]);
Heading11:=Monthcalc(Harray[11]);
Heading12:=Monthcalc(Harray[12]);
Heading13:=Monthcalc(Harray[13]);
// showmessage(DateToStr(startdate));
// showmessage(DateToStr(enddate));
// Showmassage('test');
end;
Function Monthcalc(Amonth:integer):String;
begin
Monthname[1]:='Jan';
Monthname[2]:='Feb';
Monthname[3]:='Mar';
Monthname[4]:='Apr';
Monthname[5]:='May';
Monthname[6]:='Jun';
Monthname[7]:='Jul';
Monthname[8]:='Aug';
Monthname[9]:='Sep';
Monthname[10]:='Oct';
Monthname[11]:='Nov';
Monthname[12]:='Dec';
Monthname[13]:='LY';
Result:=Monthname[Amonth];
// showmessage(DateToStr(startdate));
// showmessage(DateToStr(enddate));
// Showmassage('test');
end;

May I first suggest that you change month name array to a constant:
const
MonthNames: array[1..12] of string = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
Next I suggest some changes to avoid a hardcoded time span of 13 months, because as you are now experiencing, requirements do change and hardcoded stuff is always harder to change than variable stuff.
Declare a variable e.g. MonthSpan: integer to indicate how many months should be included (in the future a new requirement of alternative timespans of 3 months, 6 months etc are possible). Also replace all Heading1, Heading2 ... variables with a dynamic array, named Headings:
Monthspan: integer;
Headings: array of string;
For now I just initialize these at the beginning
Monthspan := 13;
SetLength(Headings, Monthspan);
You have already previously calculated StartDate: TDate so using that and the existing variable M we can now write a simple loop for getting the headings:
M := MonthOf(StartDate);
for i := 0 to MonthSpan-1 do
begin
Headings[i] := MonthNames[(M + i) mod 12];
Memo1.Lines.Add(Headings[i]);
end;
MonthOf() is a function in unit System.DateUtils.
The above replaces Function MonthCalc(), and all of your code below the //Headings comment. Elsewhere in your code, where you used e.g. Heading1 you now use Headings[0] (dynamic arrays are always indexed from 0 up) etc.
I'm not sure what the purpose of HArray[] is, but it is not needed for the determination of the headings.

Related

Generate all combinations of the SUM in Ruby but only using specific amount of numbers

I am currently pulling in F1 prices from an Api, placing them into an Array. and determining what combination is less than or equal to 20. Using the below successfully:
require 'net/http'
require 'json'
#url = 'HIDDEN URL AS HAS NO RELEVANCE'
#uri = URI(#url)
#response = Net::HTTP.get(#uri)
#fantasy = JSON.parse(#response)
arr= [[#fantasy.first["Mercedes"].to_f, #fantasy.first["Ferrari"].to_f], [#fantasy.first["Hamilton"].to_f, #fantasy.first["Verstappen"].to_f]]
target = 20
#array = arr[0].product(*arr[1..-1]).select { |a| a.reduce(:+) <= target }
Where:
#fantasy = [{"Mercedes" => "4", "Ferrari" => "6.2", "Hamilton" => "7.1", "Verstappen" => "3"}]
This is successfully outputting:
[[4.0, 7.1], [4.0, 3.0], [6.2, 7.1], [6.2, 3.0]]
Eventually this will contain all F1 teams on the left side and all F1 drivers on the right (making an F1 fantasy teambuilder). But the idea is that only 1 constructor is needed and 5 drivers for the combination that should be equal or less than 20.
Is there a way to define this? To only use 1 Team (Mercedes, Ferrari etc) and 5 drivers (Hamilton, Verstappen etc) in the calculation? Obviously do not have 5 drivers included yet as just testing. So that my output would be:
[[4.0, 7.1, 3.0], [6.2, 7.1, 3.0]]
Where the constructor forms the 'base' for the calculation and then it can have any 5 of the driver calls?
My final question is, considering what I am trying to do, is this the best way to put my API into an array? As in to manually place #fantasy.first["Mercedes"].to_f inside my array brackets?
Thanks!
Not sure if I understand the question, but does this help?
arr = #fantasy.first.values.map(&:to_f)
target = 20
p result = arr.combination(2).select{|combi| combi.sum <= target}

How to loop through a set of ranges from an array inside a for loop using readtable?

I have a for loop that is calculating values using parameters from a spreadsheet. I want the for loop to loop through different ranges but I am receiving an error and I am not sure how to fix it.
e_params_components = ["'G3:G5'","'D3:D5'","'F3:F5'","'E3:E5'"];
h_params_components = ["'G6:G8'","'D6:D8'","'F6:F8'","'E6:E8'"];
for i = 1 : length(Material_Names)
params_e = table2array(readtable(databaseFilename, 'Range', e_params_components(1))); % Read in width indep parameters for e- - hardcoded for GaAs
params_h = table2array(readtable(databaseFilename, 'Range', h_params_components(1))); % Read in width indep parameters for holes - hardcoded for GaAs
alpha_ = ionCoeff(params_e(1), params_e(2), params_e(3), Efield);
beta_ = ionCoeff(params_h(1), params_h(2), params_h(3), Efield);
k_ = beta_./alpha_; % Ionization coefficient ratio
k_values = k_values + k_(i);
end
I get the following error:
Error using readtable (line 198)
Unable to determine range. Range must be of the form 'A1' (cell), 'A:B' (column-select), '1:5' (row-select), 'A1:B5'
(rectangle-select), or a valid named range in the sheet.
I should be able to use the strings that are contained in e_params_copmponents to put into the read table function
You have to many quotes.
For example:
"'G6:G8'" is the string 'G6:G8' you want it to be the string G6:G8

convert fixed size array to record of same size

This might be a nobrainer but as a novice i can not get my head around it.
I have a function returning a fixed size array. I am trying to convert this array into a record of the same size
The function signature is like this:
type Control is new UInt8_Array (1 .. 2);
function readControl (Command : Control) return Control;
I am trying to get the two bytes (UInt8) into the record Contro_Status_Bytes with the following definition:
type Control_Status_MSB is
record
RES_UP : Boolean;
QMAX_UP : Boolean;
BCA : Boolean;
CCA : Boolean;
CALMODE : Boolean;
SS : Boolean;
WDRESET : Boolean;
SHUTDOWNEN : Boolean;
end record;
for Control_Status_MSB use
record
RES_UP at 0 range 0 .. 0;
QMAX_UP at 0 range 1 .. 1;
BCA at 0 range 2 .. 2;
CCA at 0 range 3 .. 3;
CALMODE at 0 range 4 .. 4;
SS at 0 range 5 .. 5;
WDRESET at 0 range 6 .. 6;
SHUTDOWNEN at 0 range 7 .. 7;
end record;
type Control_Status_LSB is
record
VOK : Boolean;
RUP_DIS : Boolean;
LDMD : Boolean;
SLEEP : Boolean;
HIBERNATE : Boolean;
INITCOMP : Boolean;
end record;
for Control_Status_LSB use
record
VOK at 0 range 1 .. 1;
end record;
type Control_Status_Bytes is
record
HighByte : Control_Status_MSB;
LowByte : Control_Status_LSB;
end record;
I think it must be possible to convert the array to the record and vice versa without an unchecked conversion. But currently i am missing something.
Update: This might be an valid answer/way to do that i came up after reading #Simons answer.
function readControl (Command : Control) return Control_Status_Bytes is
CSB : Control_Status_Bytes;
begin
-- do stuff return UInt8_Array of size 2 as response
CSB.HighByte := response'First;
CSB.LowByte := response'Last;
return CSB;
end readControl;
Unchecked conversion is the usual way.
But for I/O ports and peripheral registers in MCUs (Atmel AVR, MSP430 etc) which can be addressed either as numbers, or arrays of booleans (or potentially, records) there's a hack ...
p1in : constant unsigned_8; -- Port 1 Input
Pragma Volatile(p1in);
Pragma Import(Ada, p1in); -- see ARM C.6 (13)
For p1in'Address use 16#20#;
p1in_bits : constant Byte; -- Port 1 Input Bits
Pragma Volatile(p1in_bits);
Pragma Import(Ada, p1in_bits);
For p1in_bits'Address use 16#20#;
This maps the inputs from I/O port 1 to the same address, viewed either as an 8 bit Unsigned or as a Byte (an array of 8 booleans).
The equivalent in your case would be something like
For Control_Status_Record'Address use Control_Status_Array`Address;
Note you probably need to attach "pragma volatile" to both views, as here, so that changes to one view aren't lost because the other view is cached in a register.
All in all, I recommend Unchecked_Conversion over this approach. It's designed for the job and avoids messing with Volatile.
It has to depend on what happens inside readControl, but couldn't you make it return the type you want directly?
function readControl (Command : Control) return Control_Status_Bytes;
(I expect that Command actually has some structure too?).
By the way, you only define the position of one component (VOK) in Control_Status_LSB, which leaves the rest up to the compiler.
The hint from #Simon Wright pointed me in the right direction.
This is what is use now and it works:
function convert (ResponseArray : Control) return Control_Status_Bytes is
Result : Control_Status_Bytes with
Import, Convention => Ada, Address => ResponseArray'Address;
begin
return Result;
end convert;

Create array from given array

Is it possible to create an Array from another Array?
Lang: Ruby on Rails
Case
Workers are entitled to fill in their own work hours. Sometimes they forget to do it. This is what I want to tackle. In the end, I want an Array with time codes of periods the worker forgot to register his hours.
timecodes = [201201, 201202, 201203, 201204, 201205, 201206, 201207, 201208, 201209, 201210, 201211, 201212, 201213, 201301, 201302, 201304, 201305, 201306, ...]
Worker works from 201203 to 201209 with us.
timecards = [201203, 201204, 201205, 201207, 201208, 201209]
As you see, he forgot to register 201206.
What I want to do
# Create Array from timecode on start to timecode on end
worked_with_us = [201203, 201204, 201205, 201206, 201207, 201208, 201209]
#=> This is the actual problem, how can I automate this?
forgot_to_register = worked_with_us.?????(timecards)
forgot_to_register = worked_with_us - timecards # Thanks Zwippie
#=> [201206]
Now I know which period the worker forgot to register his hours.
All together
How can I create an Array from another Array, giving a start and end value?
You can just subtract arrays with - (minus):
[1, 2, 3] - [1, 3] = [2]
To build an array with years/months, this can be done with a Range, but this only works if you build an array for each year, something like:
months = (2012..2013).map do |year|
("#{year}01".."#{year}12").to_a.collect(&:to_i)
end.flatten
=> [201201, 201202, 201203, 201204, 201205, 201206, 201207, 201208, 201209, 201210, 201211, 201212, 201301, 201302, 201303, 201304, 201305, 201306, 201307, 201308, 201309, 201310, 201311, 201312]
And for the function to create those ranges dynamically:
def month_array(year_from, year_to, month_from=1, month_to=12)
(year_from..year_to).map do |year|
# Correct from/to months
mf = year_from == year ? month_from : 1
mt = year_to == year ? month_to : 12
(mf..mt).map do |month|
("%d%02d" % [year, month]).to_i
end
end.flatten
end
Update: You wanted other input parameters for this method, but I hope you can work that out yourself. :)

Way to assign multiple files to set variable names?

Is there a way to assign file names to set varibles using a GUI? Say I have 6 file sets which contain 4 colors each (blue, green, nir, red). There are 24 files in total, so i'd need 24 variables. And I want the set varialbes to be something like
blue1
green1
nir1
red1
blue2
green2
nir2
red2
etc...
Currently I'm trying to use GUIDE to creat a custom GUI that will allow the user to select the files they wish and have them assigned to certain variables. I am thinking something along the lines of having 24 popupmenus that are attached to a file directory and allows the user to select which file they want, and then it will assign that file and it's path to a variable (blue1 for example) I also want 24 check boxes to associate with an if statement
Let's say popupmenu1 is associated with the variable blue1 and checkbox1
if checkbox1 == checked
do import
elseif checkbox1 == unchecked
fill with zeros
I have the basic frame of the GUI created, I am just unclear on how to apply the file select and then associate the if statements, etc...
If you know the variable files in advance, it's bad practice (look also here and here) to use string defined variable names like this:
var1name = 'blue';
var2name = 'red';
% etc.
% load data
datablue=rand(4,1);
datared =rand(4,1);
% assign
eval([var1name '1 = datablue(1);']);
eval([var2name '1 = datared (1);']);
% etc.
eval([var1name '2 = datablue(2);']);
eval([var2name '1 = datared (2);']);
% etc
It's much easier and better to just use an ordinary array, given the variable name is not changing or application dependend, which in my example I already have as datablue and datared.
Another option if you'd like user defined variable names is to use an array of structs:
var1name = 'blue';
var2name = 'red';
sample(1).(var1name) = datablue(1);
sample(1).(var2name) = datared (1);
% ...
sample(2).(var1name) = datablue(2);
sample(2).(var2name) = datared (2);
Try some of these out, and only if you have a very good reason, resort to eval!
for k = 1:6
blue(k) = sprintf('blue%d', k);
green(k) = sprintf('green%d', k);
nir(k) = sprintf('nir%d', k);
red(k) = sprintf('red%d', k);
end
This will create the variable names for you. Then you can use assignin (i believe) or eval to set the values to the variable names.

Resources