CAPL: Create a lookup table, that assigns values to a changing variable - variable-assignment

I want to assign a value to a variable in CANoe by using a lookup table.
If sysvar:test changes to the values 1, 2 or 3, then I want that sysvar::show changes to the values 4, 78 and 33 or other values I assign. How can I do this?
Thanks in advance!

This would be a good place to start
variables
{
int mapValues[int64];
}
on prestart
{
mapValues[1] = 4;
mapValues[2] = 78;
mapValues[3] = 33;
}
on sysvar test
{
#sysvar::show = mapValues[#sysvar::test];
}

Related

Default dictionary value in Swift

I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple
i.e.
var freq = [Int:(Int,Int,Int)]()
I want to make a default value
freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )
This produces the frequency table with 7,0,0 for every value when the key does not exist.
Can I use the default for a more complicated dictionary?
For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table
var freq = [Int:Int]()
for num in nums {
freq[num, default: 0] += 1
}
We want to store the initial position of each number, and the final position of each number in the frequency table so use
var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated() {
if freq[num.element] == nil {
freq[num.element] = (1,num.offset, num.offset)
} else {
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)
}
}
For this code I want to use a default value.
I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.
I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.
Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)
Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.
var freq = [Int:(Int,Int,Int)]()
var nums = [1,2,2,3,3,3]
for num in nums.enumerated()
{
let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)
}
print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]

I seem to have an infinite while loop in my Swift code and I can't figure out why

var array: [Int] = []
//Here I make an array to try to dictate when to perform an IBaction.
func random() -> Int {
let rand = arc4random_uniform(52)*10+10
return Int(rand)
}
//this function makes a random integer for me
func finalRand() -> Int {
var num = random()
while (array.contains(num) == true){
if (num == 520){
num = 10
}else {
num += 10
}
}
array.append(num)
return num
}
The logic in the while statement is somewhat confusing, but you could try this:
var array:Array<Int> = []
func finalRand() -> Int {
var num = Int(arc4random_uniform(52)*10+10)
while array.contains(num) {
num = Int(arc4random_uniform(52)*10+10)
}
array.append(num)
return num
}
This way there will never be a repeat, and you have less boiler code.
There is probably a better method involving Sets, but I'm sorry I do not know much about that.
A few things:
Once your array has all 52 values, an attempt to add the 53rd number will end up in an infinite loop because all 52 values are already in your array.
In contemporary Swift versions, you can simplify your random routine to
func random() -> Int {
return Int.random(in: 1...52) * 10
}
It seems like you might want a shuffled array of your 52 different values, which you can reduce to:
let array = Array(1...52).map { $0 * 10 }
.shuffled()
Just iterate through that shuffled array of values.
If you really need to continue generating numbers when you’re done going through all of the values, you could, for example, reshuffle the array and start from the beginning of the newly shuffled array.
As an aside, your routine will not generate truly random sequence. For example, let’s imagine that your code just happened to populate the values 10 through 500, with only 510 and 520 being the final possible remaining values: Your routine is 51 times as likely to generate 510 over 520 for the next value. You want to do a Fisher-Yates shuffle, like the built-in shuffled routine does, to generate a truly evenly distributed series of values. Just generate array of possible values and shuffle it.

Shortcut for declaring many variables in Swift

Is there a more efficient way to write this code considering 12 variables all have the same value.
var b1 = 0, b2 = 0 , b3 = 0, b4 = 0, b5=0,b6=0,b7=0,b8=0,b9=0,b10=0,b11=0,b12=0
If you are okay with using an array instead, you might want to use this shortcut:
var b = [Int](repeating: 0, count: 12)
From struct Array documentation:
Creates a new array containing the specified number of a single, repeated value.

MATLAB: Copying variables from table to struct based on certain criteria

I have a table
column1data = [11; 22; 33];
column2data = [44; 55; 66];
column3data = [77; 88; 99];
rows = {'name1', 'name2', 'name3'};
T = table(column1data, column2data, column3data);
T.Properties.RowNames = rows
column1data column2data column3data
name1 11 44 77
name2 22 55 88
name3 33 66 99
and a struct array
S(1).rownamefield = 'name3';
S(2).rownamefield = 'name1';
S(3).rownamefield = 'name2';
S(1).columnnumberfield = 1;
S(2).columnnumberfield = 3;
S(3).columnnumberfield = 2;
S(1).field3 = [];
S(2).field3 = [];
S(3).field3 = [];
rownamefield columnnumberfield field3
1 'name3' 1 []
2 'name1' 3 []
3 'name2' 2 []
The struct array S contains criteria needed to pick the variable from table T. Once the variable is picked, it has to be copied from table T to an empty field in struct S.
S(1).rownamefield contains the name of the row in table T where the target variable resides. S(1).columnnumberfield contains the number of the column in table T with the target variable. So S(1).rownamefield plus S(1).columnnumberfield are practically the coordinates of the target variable in table T. I need to copy the target variable from table T to field3 in the struct array: S(1).field3. This has to be done for all structs so it might need to be in a for loop, but I am not sure.
The output should look like this:
rownamefield columnnumberfield field3
1 'name3' 1 33
2 'name1' 3 77
3 'name2' 2 55
I have no idea how to approach this task. This is, of course, a simplified version of the problem. My real data table is 200x200 and the struct array has over 2000 structs. I will greatly appreciate any help with this.
You could do something like the following.
First convert the rownamefield and columnnumberfield fields to cells and arrays to use as indices for the table.
rows = {S.rownamefield};
cols = [S.columnnumberfield];
subtable = T(rows, cols);
This gives you a square table which you can then convert to a cell and take the diagonal elements which are the ones you care about.
values = table2cell(subtable);
values = values(logical(eye(numel(rows))));
Then this gives a cell array of the values corresponding to the entries in S. We can then assign them
[S.field3] = deal(values{:});
disp([S.field3])
33 77 55
This would be much easier if table had an equivalent to sub2ind.
% Extract table data and linearly index it
tdata = T{:,:};
[~,row] = ismember({S.rownamefield}, T.Properties.RowNames);
col = [S.columnnumberfield];
pos = sub2ind(size(tdata),rowpos, col);
val = tdata(pos);
% Assign to struct
for ii = 1:numel(S)
S(ii).field3 = val(ii);
end
Instead of the for-loop, you can use Suever's solution with the deal() to assign values in one go (have to num2cell(val) first). Whatever is faster and more intuitive.

How does one use 4 pulldown selections to display a result text?

If I need to use 4 different drop boxes and the value of each dropbox selection which when combined would allow me to equate a textbox output --how would I do that?
I select from drop box 1, then from dropbox 2, then from dropbox 3, then from dropbox 4 and the combined selections (if drpbx1selected value = 1, and drpbx2selected value=3, and...) allows me to assign a text output to it?
I set each dropdown list in a form with a Function Option1,2,3,4 and a variable of sel1,2,3,and 4 to indicate the selected 4 possible values:
function setOption1(chosen) {
if (chosen == "1") {
var e1 = document.getElementById("e27");
var sel1 = e1.options[e1.selectedIndex].value;
}
else if (chosen == "2") {
var e2 = document.getElementById("e27");
var sel2 = e2.options[e2.selectedIndex].value;
}
}
I then used the selx variable of the functions to display the part number resulting from the 4 selected values. Here's where I'm stuck --I don't know what to use to be able to display the part number derived from the four values. There are 15 possible part numbers with 15 possible value selections.
function displaysel()
if (sel1 == "1" , sel3 == "3", sel7 == "7", sel9 == "9") {
var partno = "600-618";
}
I am new at this and really need to get this resolved. Any help is appreciated!!