What will a break-statement do in Axapta? - axapta

I have 2 while loops, and in the second there is a break; (see the code below)
My question is: will the break cause a stop in the second loop or for the 2?
while select dirPartyRelationship
join dirPartyTable
where dirPartyTable.RecId == dirPartyRelationship.ChildParty
join dirPersonName
where dirPersonName.Person == dirPartyTable.RecId
{
while select checkDirRelationship
where checkDirRelationship.ChildParty == dirPartyRelationship.RecId
{
if (checkDirRelationship.RelationshipTypeId == _relationshipType)
{
break;
}
}...

The break will only break out of the current code block.
Create a job and use this sample code;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
break;
}
info(strfmt("outer loop count %1",i));
}
You will see a quick example of j never getting over 0, but being printed 100 times.
Edit;
If you want to break out of the nested loop you could work around by declaring a boolean, maybe called breakAll, and setting breakAll to true before the break; line in the inner loop. In the outer loop check for breakAll like this;
for(i=0; i<100; i++)
{
for(j=0; j<100; j++)
{
info(strfmt("inner loop count %1",j));
if (somethingToCheck)
{
breakAll = true;
break;
}
}
info(strfmt("outer loop count %1",i));
if (breakAll)
{
break;
}
}

Related

Function Vector<Vector<...>> : Entries getting changed and i dont know why

I want to initialize a square of textures with sf::sprite elements.
To make the main()-function as clean as possible, I wrote a function in another file, it looks like this:
vector<vector<sf::Sprite>> init_board_graphics(int size) {
sf::Texture _t1;
sf::Texture _t2;
_t1.loadFromFile("images/whitefield.png");
_t2.loadFromFile("images/blackfield.png");
vector<vector<sf::Sprite>> board;
//init vector for insert row-values
vector<sf::Sprite> help_vector;
for (int i = 0; i < size; i++) {
help_vector.clear();
for (int j = 0; j < size; j++) {
sf::Sprite insert_element;
if ((i + j) % 2 == 0) {
insert_element = sf::Sprite(_t1);
}
else {
insert_element = sf::Sprite(_t2);
}
insert_element.setPosition(i * pixel_field, j * pixel_field);
help_vector.push_back(insert_element);
}
board.push_back(help_vector);
}
return board;
}
When I call the function in main(), everything is fine at first, the elements are at the correct positions and the textures are also of correct size.
When I try to debug the next steps, I can observe that the textures of the elements are getting changed in size, even though the next lines have absolutely nothing to do with my object. First, I thought the entries are getting changed because I allocate the entries in the <vector<vector<sf::sprite>> - object in a wrong way but I fixed this:
if (parameters::visual_output) {
//Draw field
for (int i = 0; i < size_board; i++) {
vector<sf::Sprite> help_vec = board[i];
for (int j = 0; j < size_board; j++) {
window.draw(help_vec[j]);
}
}
}
The textures are still getting changed for some reason. Why is this happening?

How to compare values between a 2 dimensional and one dimesional array?

Array1:
[[P01, 153425], [P02, 3951990], [P03, 106658], [P04, 4563594], [P05, 60198], [P07, 326292], [P08, 1265], [P09, 108293], [P10, 183698], [P11, 5084]] this comes from BigQuery.
Array2:
[[P01], [P02], [P03], [P04], [P05], [P06], [P07], [P08], [P09], [P10], [P11]]
this array is fetched from google sheet column using sheet.getSheetValues(3,1,11,1);
Based on this i should update a column in google sheet based on the matching values between Array1 and Array2:
eg: [153425,3951990,106658,4563594,60198,326292,1265,108293,183698,5084]
if value does not match the corresponding value should remain empty.
Compare Array1[i][0] with Array2[j] inside for loop, if it is equal take the value of Array1[i][1]
for(var i=0; i<Array1.length; i++){
for(var j=0; j<Array2.length; j++){
if(Array1[i][0] == Array2[j]){
//do something
//if it can be equal to only one number, you can break it here
}
}
}
EDIT
Then you should compare Array2[i] with Array1[j][0]
for(var i=0; i<Array2.length; i++){
var isExist = false;
for(var j=0; j<Array1.length; j++){
if(Array2[i] == Array1[j][0]){
//do something
//if it can be equal to only one number, you can break it here
isExist = true;
}
}
if(!isExist){
//do something if it does not exist
}
}

if (this.isSimulation) return;

I don't understand the usage of code where there's no block of code after if (..isSimulation) that instead just does return after which it follows up with an actual code that is suppose to access restful api from another server.
Right so:
if (this.isSimulation) return;
var charsInfo = new Array();
_keys.forEach(function (key, index, array) {
EveOnline.fetch('account:Characters', { keyID: key.key1, vCode: key.key2 }, Meteor.bindEnvironment(function(err, result) {
if (err) {
In most C-based languages you can omit the code block ({ and } brackets) if the inner block evaluates to one statement.
For instance this:
if (x == 0) {
x++;
}
and this:
if (x == 0)
x++;
do exactly the same thing. This works also for nested conditionals, like in your example.
So this:
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i>j) {
x++;
}
}
}
and this:
for (i=0; i<10; i++)
for (j=0; j<10; j++)
if (i>j)
x++;
are also the same thing. The nested conditional/loop is seen as a single evaluated "thing" until it gets to the end. Basically, the brackets defining the code blocks are assumed in the second example.
What you can't do is have a multi line code block not be surrounded in curlies.
For instance this:
if (i>j) {
x++;
y++;
}
And this:
if (i>j)
x++;
y++;
do NOT do the same thing at all. In the first case, x and y are both incremented only if i is greater than j. In the second case, x will be incremented only if i is greater than j, but y gets incremented all the time as it's not part of the if conditional before it. This is because x++ is considered one "statement" and y++ is considered another, and only the first statement is assumed to be part of the proceeding if block.

How to display new List item?

next 2 Mp3's on the list, can someone help me with this?
I want to display all the Mp3's that are left
You could use Console.ReadKey to wait for any input key, and use the same logic to print rest of the records.
int maxCount = 3;
for (int i = 0; i < maxCount && i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
Console.ReadKey(); // wait for key-in
// print remaining records.
for (int i = maxCount ; i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
You could use the Console.ReadKey and loop through the list again and start from the index where you left off initially.
for (int i = maxCount; i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
You could also make it a seperate method so you'll only have one place in your code to loop through your list and you can easily modify how many items you want to print and from what index to start printing.
public void ShowItemsInConsole(int firstIndex, int lastIndex)
{
for (int i = firstIndex; i < lastIndex && i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
}

Iterating over basic “for” loop using Handlebars.js

I’m new to Handlebars.js and just started using it. Most of the examples are based on iterating over an object. I wanted to know how to use handlebars in basic for loop.
Example.
for(i=0 ; i<100 ; i++) {
create li's with i as the value
}
How can this be achieved?
There's nothing in Handlebars for this but you can add your own helpers easily enough.
If you just wanted to do something n times then:
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i)
accum += block.fn(i);
return accum;
});
and
{{#times 10}}
<span>{{this}}</span>
{{/times}}
If you wanted a whole for(;;) loop, then something like this:
Handlebars.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
and
{{#for 0 10 2}}
<span>{{this}}</span>
{{/for}}
Demo: http://jsfiddle.net/ambiguous/WNbrL/
Top answer here is good, if you want to use last / first / index though you could use the following
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i) {
block.data.index = i;
block.data.first = i === 0;
block.data.last = i === (n - 1);
accum += block.fn(this);
}
return accum;
});
and
{{#times 10}}
<span> {{#first}} {{#index}} {{#last}}</span>
{{/times}}
If you like CoffeeScript
Handlebars.registerHelper "times", (n, block) ->
(block.fn(i) for i in [0...n]).join("")
and
{{#times 10}}
<span>{{this}}</span>
{{/times}}
This snippet will take care of else block in case n comes as dynamic value, and provide #index optional context variable, it will keep the outer context of the execution as well.
/*
* Repeat given markup with given times
* provides #index for the repeated iteraction
*/
Handlebars.registerHelper("repeat", function (times, opts) {
var out = "";
var i;
var data = {};
if ( times ) {
for ( i = 0; i < times; i += 1 ) {
data.index = i;
out += opts.fn(this, {
data: data
});
}
} else {
out = opts.inverse(this);
}
return out;
});
Couple of years late, but there's now each available in Handlebars which allows you to iterate pretty easily over an array of items.
https://handlebarsjs.com/guide/builtin-helpers.html#each

Resources