Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Loop not ending, insight as to why?

Reply
Thread Tools

Loop not ending, insight as to why?

 
 
LANkrypt0
Guest
Posts: n/a
 
      06-18-2004
I wrote the following code and for some reason it does not exit when my
$word is the same as $ARGV[0].

Can anyone shed some light on this for me?
Is it because it needs to actually run through every foreach loop?

Thanks

==+ BEGIN CODE +==

#!/usr/bin/perl
use strict;

my @valpha = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
my $word;

until ($word eq $ARGV[0])
{
foreach my $letter1 (@valpha)
{
foreach my $letter2 (@valpha)
{
foreach my $letter3 (@valpha)
{
foreach my $letter4 (@valpha)
{
foreach my $letter5 (@valpha)
{
$word = $letter1.$letter2.$letter3.$letter4.$letter5;
print "$word\n";
}
}
}
}
}
}
print "Word found!\n";

==+ END CODE +==

--
LANkrypt0 :: aa#2118
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-19-2004
LANkrypt0 wrote:
> I wrote the following code and for some reason it does not exit when
> my $word is the same as $ARGV[0].
>
> Can anyone shed some light on this for me?
> Is it because it needs to actually run through every foreach loop?
>
> Thanks
>
> ==+ BEGIN CODE +==
>
> #!/usr/bin/perl
> use strict;
>
> my @valpha = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
> "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
> "z"); my $word;
>
> until ($word eq $ARGV[0])
> {
> foreach my $letter1 (@valpha)
> {
> foreach my $letter2 (@valpha)
> {
> foreach my $letter3 (@valpha)
> {
> foreach my $letter4 (@valpha)
> {
> foreach my $letter5 (@valpha)
> {
> $word = $letter1.$letter2.$letter3.$letter4.$letter5;
> print "$word\n";
> }
> }
> }
> }
> }
> }
> print "Word found!\n";
>
> ==+ END CODE +==


You got your program logic totally messed up.
For the sake of argument imagine that the "until" does not exist.
Now, how often is the outermost "foreach" being executed? Or to rephrase the
question, what would be the last letter, that is processed by the outermost
loop?

The same logic applies to the inner loops, too, which ultimately means that
$word has a value of "zzzzz" when the outermost foreach terminates.

Only now after all the inner loops are done will the "until" have a chance
to kick in and to check if the condition is satisfied. Obviously it is not
(unless you program argument happens to be "zzzzz").
Therefore the whole foreach loop chaos starts over again.

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
GregorianCalendar - it's a bomb ;-) [code insight] Xandau Java 9 06-25-2004 12:45 PM
[ANN] OneClickRevelation: JDX Provides Instant and Interactive Insight Into Your Data Damodar Periwal Java 0 02-29-2004 07:53 AM
insight needed: any way to have Java run a perl script.. Falco98 Perl 0 09-15-2003 12:36 AM
How to ensure code insight? Bura Tino Java 1 08-31-2003 04:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57