top of page
Search

Array initialization list

Writer's picture: Ivaylo FizievIvaylo Fiziev

It has been a while since we allow the usage of arrays in SCL but initialization was a pain until now. Usually you had to populate the values within the array in the body of the block which is not very convenient. Also you had to make sure this happens only once at the beginning of the simulation. Not anymore :)

Now (v2502) you can simply use an array initializer list to do the heavy job for you.

You just provide the values of the array as part of its declaration:

arr:Array [1..3,1..3] of Bool := [
	true, false, true, 
    false, BOOL#1, false,
    BOOL#1, false, BOOL#1 
];

That's it! The initialization happens when the variable is allocated. Period.

Of course there are some things to consider:


Q: What happens if the list of values in the initialization list are not enough to fill each and every element of the array?

A: The ones that have no value specified take the default value for the type.

Q: What is the order of the elements in the list when it comes to multi- dimensional arrays?

A: The innermost dimension is populated first. e.g. the first element in the list becomes the first element of the innermost dimension.


Q: What happens if I mix the literals in the list? For example BOOL#True and DINT#123.

A: The end result depends on the array type.


Q: How should I express a lot of repetitive values in the list? Is there a simple way to do it?

A: An alternative form of the syntax allows you to do this. Here you provide the value as well as the number of consecutive elements that should have this value. However this syntax is still not supported!

arr:Array [0..2, 0..4, 0..1] of Word := [2(16#0), 7(16#1), 16#55 ];

Do you recall the post about the seven segment display with the truth table? Now we ca implement it much better. Just use the initialization list and remove the array initialization in the body of the block. This not only makes the code shorter but also makes it faster.

truthTable : Array [0..9, 1..7] of BOOL := [ 
	TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,
	FALSE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,
	TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,
	TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE,
	FALSE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,
	TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,TRUE,
	TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,
	TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,
	TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
	TRUE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE
];

jointNames : Array [1..7] of STRING := [
	'j_a','j_b','j_c','j_d','j_e','j_f','j_g'
];

Try it!

2 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Subscribe Form

Thanks for submitting!

bottom of page