http://qs1969.pair.com?node_id=11139196

dnamonk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have a general logic question. I need help in logic and not code. I have a question about recalling my function within a loop. Below is my code:
List_new = myfunction() for items in List_new: if(my condition is TRUE): Execute some commands if(my condition is FALSE): recall myfunction()
My problem is that I am loading "List_new" using myfunction(). How can I change "List_new" iteratively when my condition is False. I want to reload the function especially when the condition is FALSE. This means I keep calling the function until it is false and then execute the final output from myfunction(). Thank you in advance for your help.

Replies are listed 'Best First'.
Re: Recalling a function iteratively
by tybalt89 (Monsignor) on Nov 29, 2021 at 03:16 UTC

    In untested pseudo-perl

    my @list_new = myfunction(); while( @list_new ) { my $item = shift @list_new; if( conditiontrue ) { execute($item); } if( conditionfalse ) { @list_new = myfunction(); } }
      Thank you for the advice :)
Re: Recalling a function iteratively
by NetWallah (Canon) on Nov 29, 2021 at 03:17 UTC
    Your "Logic" is too fuzzy to respond meaningfully.

    It is a bad idea to iterate over a list, and attempt to modify the list during the iteration.

    My advice is to define a deterministic termination condition, and put your logic in a "while" loop.

                    "If you had better tools, you could more effectively demonstrate your total incompetence."

Re: Recalling a function iteratively
by AnomalousMonk (Archbishop) on Nov 29, 2021 at 05:05 UTC

    Here's my guess:

    c:\@Work\Perl>perl -wMstrict -le "use List::MoreUtils qw(any); ;; sub myfunc { return map { int rand 10 } 1 .. $_[0]; } ;; my @List_new; ;; UNTIL_TRUE: { @List_new = myfunc(7); any { $_ == 3 } @List_new or redo UNTIL_TRUE; } ;; print qq{@List_new}; " 0 1 6 1 8 1 3
    Notes:
    • List::MoreUtils::any() can be replaced with the built-in grep for short lists (for some definition of "short").
    • any() may also be found in List::Util in later versions of Perl.
    • The label UNTIL_TRUE is not needed, it's just documentary.


    Give a man a fish:  <%-{-{-{-<