sivaratna has asked for the wisdom of the Perl Monks concerning the following question:
I am so sorry as its my first posting I was not aware of these things. Thank you so much for these many suggestions. I don't want you guys to bother much about my real coding but, I am posting sample code with same problem. If I will be able to solve problem with this sample code then I can be able to do the same for my original code as well.
Here is my code: and the problem is: I am trying to match the $string1 by using permutations of LHS and RHS and then joining LHS and RHS to be able to match with my string. So, I have to apply permute on LHS and RHS. My first while loop(@res) is for LHS and then inner loop(@res1) is for RHS. It is taking only first iteration value from outer loop and taking all values from inner loop.
OUTPUT:#! /usr/bin/perl use warnings; use strict; use Algorithm::Permute; my $string1="acbd = 4213"; my $alp = new Algorithm::Permute(['a'..'d']); my $num = new Algorithm::Permute(['1'..'4']); my (@res,@res1,$alp1,$alp2,$fin); while (@res = $alp->next){ $alp1=join('',@res); while(@res1 = $num->next){ $alp2 = join('',@res1); $fin = join('',$alp1,' = ',$alp2); print"$fin\n"; if($fin eq $string1){ print"String exists"; } } }
dcba = 4321 dcba = 3421 dcba = 3241 dcba = 3214 dcba = 4231 dcba = 2431 dcba = 2341 dcba = 2314 dcba = 4213 dcba = 2413 dcba = 2143 dcba = 2134 dcba = 4312 dcba = 3412 dcba = 3142 dcba = 3124 dcba = 4132 dcba = 1432 dcba = 1342 dcba = 1324 dcba = 4123 dcba = 1423 dcba = 1243 dcba = 1234
As you can see its taking only one iteration from LHS i.e., outer loop and taking all values from inner loop. I want it to take all possible values for RHS i.e., outer while loop as well.
I think I am clear and my question is clear. Please let me know if you need any clarifications.
Thank you so much for all your invaluable suggestions. Please help he with this....awaiting for your awesome replies...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: nested while loops
by stevieb (Canon) on Jun 22, 2015 at 18:02 UTC | |
|
Re: nested while loops
by Laurent_R (Canon) on Jun 22, 2015 at 19:06 UTC | |
|
Re: nested while loops
by NetWallah (Canon) on Jun 22, 2015 at 19:05 UTC | |
by sivaratna (Initiate) on Jun 23, 2015 at 06:19 UTC | |
by choroba (Cardinal) on Jun 23, 2015 at 10:58 UTC | |
|
Re: nested while loops
by 1nickt (Canon) on Jun 22, 2015 at 19:13 UTC | |
|
Re: nested while loops
by Laurent_R (Canon) on Jun 22, 2015 at 17:49 UTC |