cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

SKIPPING PARTICULAR LOOPS in Programming

ppal
17-Peridot

SKIPPING PARTICULAR LOOPS in Programming

Hi

How do I select which particular loops to skip. Depending on a condition I may want to skip loop "k" or "j" or "k" AND "j" and just get the next "i"

How do I go about doing that in similar scenario to below:

ppal_0-1651108917124.png

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:ppal)

Can you explain what the reasoning behind your mod condition is?

If the only goal is to eliminate the rows which contain at least one character of "Reject", this function might do the job:

Werner_E_0-1651576913712.png

The function is not ORIGIN-aware, so ORIGIN=0 is assumed.

The "break" isn't mandatory. If you delete it, the function is just working a little bit more inefficient because every time ALL characters in "Reject" are tested even after a character was already found in the string.

View solution in original post

14 REPLIES 14
ValeryOchkov
24-Ruby IV
(To:ppal)

Yo can use not the for but the while loop.

Werner_E
25-Diamond I
(To:ppal)

How about putting a "break" command in the else branch and also an "if Flag=1  break" after the k-loop to also skip the j-loop ?

ppal
17-Peridot
(To:Werner_E)

I think the break function will work for me but I will need to do the following in the programming

 

Run a for loop

 

Read a file (Has rows of data)

Carry out Operations (This results in some rows of data removed)

I want to use the “new” data file to be read read again  ( I dont know how to create the file and then get it to read it as a new file for the next iteration)

 

Keep doing till loop finished and data weeded out.

 

I think it may be an append or just a new writetext function function ?

 

Thanks for your help.

Werner_E
25-Diamond I
(To:ppal)

From your description I see no need to write the intermediate data to a new file in each iteration step just to read in this file in the next iteration.
Wouldn't it suffice to read in the file just at the beginning and only write the final data to a new file at the end ?

Which command you use to read and write the files depends on the data format of the file you have - csv, excel, text in columns, ....

ppal
17-Peridot
(To:Werner_E)

 

 

This is where I need to set "Ans" to be reallocated the new file/data (ie this will now be the new file to be read

ppal_2-1651200775059.png

So how do I make the Ans become the  new "String"?

Werner_E
25-Diamond I
(To:ppal)


So how do I make the Ans become the  new "String"?


After the complete vector "Ans" has been calculated you would simply write "String <- Ans",           overwriting "String" and so should have the same effect as writing "Ans" to a file and immediately reading it in to variable "String".

The only problem may be the index "reject" which determines the element in vector "String" used to compare with all the other elements of "String". Your code snippet dos not show how this index would be derived and if it changes with every iteration step or if its always the same constant string (in which case you would assign it a normal variable at the top of the program and use this for all subsequent comparisons).

 

One additional minor points I noticed: Should "Temp" really be an indexed variable? Do you really need to keep all different values? Wouldn't a normal variable (which then is overwritten in every step) suffice?

ppal
17-Peridot
(To:Werner_E)

The index i is is from 0 to how many rows are in the file. So it will change as data is removed. Temp does not need index

Werner_E
25-Diamond I
(To:ppal)


@ppal wrote:

The index i is is from 0 to how many rows are in the file. So it will change as data is removed.


Yes, the loop starts from a new after the assignment "String <- Ans" was done

 

 


Temp does not need index

OK, I thought so.

ppal
17-Peridot
(To:Werner_E)

Hi

The index "reject" is fixed as works ok. The isssue is with "i" The string index as this value changes as the file becomes smaller.

ppal_1-1651531370986.png

 

ppal_0-1651531347361.png

 

Werner_E
25-Diamond I
(To:ppal)

You may consider posting your worksheet along with a sample data. Its cumbersome to debug a picture.
It also would help if you describe in detail you would like to achieve with your program.

 

Nonetheless some remarks:

You should not use string[reject but rather a separate variable because String changes in the program loop.

In the last if you redefine String in a way so that String probably gets less elements so the outer loop eventually must fail. It may help to change the outer loop to a while loop using an appropriate condition.

ppal
17-Peridot
(To:Werner_E)

Prime 7 and Sample Hex file attached.

StuartBruff
23-Emerald III
(To:ppal)

Both of your breaks only exit the "k" loop.   After, for example, your program finds the first "reject" character, it goes and looks for the second "reject" character.  If that character isn't in the string and your modulus condition is met, then the program will add the string to Ans.   You need to arrange your code so that a rejection of a character exits both the "j" and "k" loops.

 

Stuart

Werner_E
25-Diamond I
(To:ppal)

Can you explain what the reasoning behind your mod condition is?

If the only goal is to eliminate the rows which contain at least one character of "Reject", this function might do the job:

Werner_E_0-1651576913712.png

The function is not ORIGIN-aware, so ORIGIN=0 is assumed.

The "break" isn't mandatory. If you delete it, the function is just working a little bit more inefficient because every time ALL characters in "Reject" are tested even after a character was already found in the string.

StuartBruff
23-Emerald III
(To:Werner_E)

And here's a variation on the theme, for purely pedagogical purposes (and to squeeze in an alliteration).

 

The example shows both the use of a local function to do the checking and of the for loop to iterate over a vector without the need for a loop iteration variable.  It also demonstrates the use of boolean operations to update the keep/reject variable.

 

2022 05 03 a.png

 

The (slightly) modified version of the function below shows how a local function can effectively use a main function argument as a "global" variable, thereby removing the need to pass the rejection string as an argument to the local function.

 

2022 05 03 b.png

 

Mathcad Prime 7 worksheet attached.

 

Stuart

Announcements

Top Tags