Quantcast

Looping through a list using while

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Looping through a list using while

Kemas Muslim
Hi, 

I am trying to loop inside a list, but I want to use "while" instead of "for" loop because I want the loop stops if it finds a member that satisfied some criteria.
Before I do that, I have tried to code a plan to find out how "while" behaves in Jason as follows:

+!test_while : true <-
L = [1,2,3];
.length(L,Len);
I = 0;
while (I < Len) {
.nth(I,L,X);
.print(X);
I = I + 1; // this causes an error
}.
 
Unfortunately, when I run the code, it gives me the following error:

"No failure event was generated for +!test_while[code((0 = 1)),code_line(78),code_src("robot.asl"),error(constraint_failed),error_msg("Constraint (I = (I+1)); .loop((I < Len),{ .nth(I,L,X); .print(X); (I = (I+1)) },{L=[1,2,3], I=0, Len=3}) was not satisfied (robot.asl:82) un={L=[1,2,3], Len=3, I=0, X=1}"),source(self)]"

I also found out that when a variable is assigned a value, it cannot be assigned with another value.

I = 0;
I = 1; // this also gives similar error

Did I do something wrong?
Is there any other approach to loop on a list and stop before it reaches the last element?

Thank you very much in advance.

Best regards,

-- 
Kemas Muslim

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Jason-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jason-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Looping through a list using while

Jomi Hubner
Dear Kemas,

indeed logical programming is “bizarre” initially and vars car not change their values, they are more like mathematical variables.

if you want do to something with all members of a list that have condition c, you can write:

        for( .member(I,List) & c(I) ) {
           …. process I …
        }

for instance, suppose the list has numbers and you want to print those greater than 20:

        for( .member(I,List) & I > 20 ) {
           .print(I);
        }


the for in Jason iterates on all solutions for the query that is given as parameter.

HTH,

Jomi

ps.: sorry for taking so long to answer.


> On 21 Jan 2016, at 02:41, Kemas Muslim <[hidden email]> wrote:
>
> Hi,
>
> I am trying to loop inside a list, but I want to use "while" instead of "for" loop because I want the loop stops if it finds a member that satisfied some criteria.
> Before I do that, I have tried to code a plan to find out how "while" behaves in Jason as follows:
>
> +!test_while : true <-
> L = [1,2,3];
> .length(L,Len);
> I = 0;
> while (I < Len) {
> .nth(I,L,X);
> .print(X);
> I = I + 1; // this causes an error
> }.
>  
> Unfortunately, when I run the code, it gives me the following error:
>
> "No failure event was generated for +!test_while[code((0 = 1)),code_line(78),code_src("robot.asl"),error(constraint_failed),error_msg("Constraint (I = (I+1)); .loop((I < Len),{ .nth(I,L,X); .print(X); (I = (I+1)) },{L=[1,2,3], I=0, Len=3}) was not satisfied (robot.asl:82) un={L=[1,2,3], Len=3, I=0, X=1}"),source(self)]"
>
> I also found out that when a variable is assigned a value, it cannot be assigned with another value.
>
> I = 0;
> I = 1; // this also gives similar error
>
> Did I do something wrong?
> Is there any other approach to loop on a list and stop before it reaches the last element?
>
> Thank you very much in advance.
>
> Best regards,
>
> --
> Kemas Muslim
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140_______________________________________________
> Jason-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jason-users


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Jason-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jason-users
Loading...