Terraform: Combine Variables to make a list - azure-devops

locals{
instance_name = "TESTWINDOWSVM"
instance_count = 4
vm_instances = format("%s%s", local.instance_name,local.instance_count)
}
I am creating a windows VM via terraform azure, I wanted to combine instance_name and instance_count and be able to create a new list variable.
output should be [ TESTWINDOWSVM001, TESTWINDOWSVM002, TESTWINDOWSVM003, TESTWINDOWSVM004]. is there a way to do this in terraform?

You can do this with a straightforward for expression iterating from a range function inside a list constructor, and some string interpolation with a format function to ensure three numbers, and both on the return:
[for idx in range(local.instance_count) : "${local.instance_name}${format("%03d", idx + 1)}"]

Related

hashTF how do I get the term & it's index

I have used HashingTF with below:
tf = HashingTF(inputCol="text_tokenized", outputCol="text_tf", numFeatures=1000)
pipeline = Pipeline(stages=[tf])
hash_fit = pipeline.fit(df_disco_train_o)
df_disco_train_h = hash_fit.transform(df_disco_train_o)
And would like to get dictionary of {index: term} or {term: index} pair
One way to get it is via
tf.indexOf("first") # will give you index of "0"
But in order to do that I need to get all unique terms:
df_term_count = df_disco_train_h.select(explode('text_tokenized').alias('exploded')).groupby('exploded').count()
which is extremely costly. Is there a somekind of metadata or some function I can call to retrieve it without a query?
Here's what the output looks like, which is List Of text & sparse matrix.

define variable by concatenating strings

I want to iteratively define a variable whose name is the concatenation of two strings.
In particular, the following code is meant to create a variable Uvel_spring that contains the values Uvel stored in the file spring_surface.mat :
seasons{1}='spring';
seasons{2}='summer';
seasons{3}='autumn';
seasons{4}='winter';
for ii=1:4
['Uvel_',char(seasons(ii))] = load([char(seasons(ii)),'_surface.mat'],...
'Uvel');
end
However, I get the following error:
An array for multiple LHS assignment cannot contain LEX_TS_STRING.
I solved it by using evalc:
for ii=1:4
evalc( sprintf(['Uvel_',char(seasons(ii)),'=','load(''',char(seasons(ii)),'_surface.mat'',',...
'''Uvel''',')']) );
end
However, it is horrible and I would like to improve the code.
Does someone have an alternative solution?
Use struct instead.
for ii=1:4
Uvel.(seasons{ii}) = load([seasons{ii},'_surface.mat'], 'Uvel');
end
You'll end up having those four seasons as the fields of Uvel. So you'll be accessing Uvel_spring as Uvel.spring and similarly for others.

MATLAB: Loop through the values of a list from 'who' function

I have a long list of variables in my workspace.
First, I'm finding the potential variables I could be interested in using the who function. Next, I'd like to loop through this list to find the size of each variable, however who outputs only the name of the variables as a string.
How could I use this list to refer to the values of the variables, rather than just the name?
Thank you,
list = who('*time*')
list =
'time'
'time_1'
'time_2'
for i = 1:size(list,1);
len(i,1) = length(list(i))
end
len =
1
1
1
If you want details about the variables, you can use whos instead which will return a struct that contains (among other things) the dimensions (size) and storage size (bytes).
As far as getting the value, you could use eval but this is not recommended and you should instead consider using cell arrays or structs with dynamic field names rather than dynamic variable names.
S = whos('*time*');
for k = 1:numel(S)
disp(S(k).name)
disp(S(k).bytes)
disp(S(k).size)
% The number of elements
len(k) = prod(S(k).size);
% You CAN get the value this way (not recommended)
value = eval(S(k).name);
end
#Suever nicely explained the straightforward way to get this information. As I noted in a comment, I suggest that you take a step back, and don't generate those dynamically named variables to begin with.
You can access structs dynamically, without having to resort to the slow and unsafe eval:
timestruc.field = time;
timestruc.('field1') = time_1;
fname = 'field2';
timestruc.(fname) = time_2;
The above three assignments are all valid for a struct, and so you can address the fields of a single data struct by generating the field strings dynamically. The only constraint is that field names have to be valid variable names, so the first character of the field has to be a letter.
But here's a quick way out of the trap you got yourself into: save your workspace (well, the relevant part) in a .mat file, and read it back in. You can do this in a way that will give you a struct with fields that are exactly your variable names:
time = 1;
time_1 = 2;
time_2 = rand(4);
save('tmp.mat','time*'); % or just save('tmp.mat')
S = load('tmp.mat');
afterwards S will be a struct, each field will correspond to a variable you saved into 'tmp.mat':
>> S
S =
time: 1
time_1: 2
time_2: [4x4 double]
An example writing variables from workspace to csv files:
clear;
% Writing variables of myfile.mat to csv files
load('myfile.mat');
allvars = who;
for i=1:length(allvars)
varname = strjoin(allvars(i));
evalstr = strcat('csvwrite(', char(39), varname, '.csv', char(39), ', ', varname, ')');
eval(evalstr);
end

Print the name of a variable on upon a plot/figure

Is it possible to refer back to/access the names of variables (say nx1 arrays) that make up a matrix? I wish to access them to insert there names into a plot or figure (as a text) that I have created. Here is an example:
A = [supdamp, clgvlv,redamp,extfanstat,htgvlv,occupied,supfanspd]
%lots of code here but not changing A, just using A(:,:)'s
%drawn figure
text(1,1,'supdamp')
...
text(1,n,'supfanspd')
I have failed in an attempt create a string named a with their names in so that I could loop through a(i,1), then use something like text(1,n,'a(i,1)')
Depending on your problem, it might make sense to use structures with dynamical field names.
Especially if your data in the array have some meaning other than just entries of a matrix in linear algebra sense.
# name your variables so that your grandma could understand what they store
A.('supdamp') = supdamp
A.('clgvlv') = clgvlv
...
fieldsOfA = fieldnames(a)
for n = 1 : numel(fieldsOfA )
text(1, n, fieldsOfA{n})
end

How can I dynamically access a field of a field of a structure in MATLAB?

I'm interested in the general problem of accessing a field which may be buried an arbitrary number of levels deep in a containing structure. A concrete example using two levels is below.
Say I have a structure toplevel, which I define from the MATLAB command line with the following:
midlevel.bottomlevel = 'foo';
toplevel.midlevel = midlevel;
I can access the midlevel structure by passing the field name as a string, e.g.:
fieldnameToAccess = 'midlevel';
value = toplevel.(fieldnameToAccess);
but I can't access the bottomlevel structure the same way -- the following is not valid syntax:
fieldnameToAccess = 'midlevel.bottomlevel';
value = toplevel.(fieldnameToAccess); %# throws ??? Reference to non-existent field 'midlevel.bottomlevel'
I could write a function that looks through fieldnameToAccess for periods and then recursively iterates through to get the desired field, but I am wondering if there's some clever way to use MATLAB built-ins to just get the field value directly.
You would have to split the dynamic field accessing into two steps for your example, such as:
>> field1 = 'midlevel';
>> field2 = 'bottomlevel';
>> value = toplevel.(field1).(field2)
value =
foo
However, there is a way you can generalize this solution for a string with an arbitrary number of subfields delimited by periods. You can use the function TEXTSCAN to extract the field names from the string and the function GETFIELD to perform the recursive field accessing in one step:
>> fieldnameToAccess = 'midlevel.bottomlevel';
>> fields = textscan(fieldnameToAccess,'%s','Delimiter','.');
>> value = getfield(toplevel,fields{1}{:})
value =
foo