473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Loop not ending, insight as to why?

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
Jul 19 '05 #1
1 1599
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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
0
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
10
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
8
by: Dave | last post by:
I am trying to write an event-driven application with no main window that runs "forever". It waits on a named event and then displays a window depending on data pased in a memory-mapped file. I...
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
7
by: UnkleVo | last post by:
Can someone run the code below and tell me why it never reaches 0.06? I am really puzzled..... or just going crazy? Dim i As Double For i = 0.01 To 0.05 Step 0.01 Debug.WriteLine(i) Next...
3
by: troy_lee | last post by:
I have a form that asks for the month, the year, the number of weeks in the month and the beginning date for the month. I want to be able to: Number the weeks (e.g. 1, 2,3, etc.) Assign a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.