Loop not ending, insight as to why?

Discussion in 'Perl' started by LANkrypt0, Jun 18, 2004.

  1. LANkrypt0

    LANkrypt0 Guest

    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
    LANkrypt0, Jun 18, 2004
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Jun 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    450
    Jim Moe
    Mar 16, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,850
    Smokey Grindel
    Dec 2, 2006
  3. Branimir Maksimovic
    Replies:
    5
    Views:
    298
    James Kanze
    Feb 7, 2010
  4. Maxim Yegorushkin
    Replies:
    1
    Views:
    484
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page