Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting iterations of a loop 1

Status
Not open for further replies.

carg1

MIS
Aug 19, 2003
20
US
Hello everyone. What I want to do is go through a loop, and when it has gone through that loop one or more times, I want it to run a subroutine. The code I have so far is this:
Code:
print "\n\nPlease enter the name of the file to load: ";
$DB = <STDIN>;
chomp($DB);

INPUT: $Choices = <<'OPTIONS';
Hello! What would you like to do with the file?

1) Filter hits by blocked content
2) Filter hits by passed content
3) Filter hits by IP address
4) Count IP address instances
5) Exit the program

OPTIONS

print &quot;\n\n$Choices&quot;;
print &quot;Your choice: &quot;;
$PickNumber = <STDIN>;
chomp($PickNumber);

$i == 0;
do {
$i ++;
if($PickNumber == 1) {
	readme();
	goto INPUT;
}
	elsif($PickNumber == 2) {
		print &quot;\n\nCan't filter hits by passed content yet.\n&quot;;
		goto INPUT;
	}
		elsif($PickNumber == 3) {
			print &quot;\n\nCan't filter hits by IP address yet.\n&quot;;
			goto INPUT;
		}
			elsif($PickNumber == 4) {
				print &quot;\n\nCan't count IP address instances yet.\n&quot;;
				goto INPUT;
			}
				elsif($PickNumber == 5) {
					print &quot;It's been a pleasure, see ya next time!\n&quot;;
					exit;
				}
} until $i != 0 || input2();
The first time the loop runs, it will give that hello message after you first load the file. After that, I want it to run another version of the block with the numbered options that allows you to open a different file as a sixth choice. Whenever I run that loop it always stays with the first iteration's hello message.
Code:
do
/
Code:
until
is my latest attempt, before that I tried
Code:
for
,
Code:
until
, and
Code:
while
loops to no avail. I know I'm just missing one simple thing. Help please?
 
If do/until is a problem, use a for or while loop. Both will execute the block once before testing the loop control value.

There's always a better way. The fun is trying to find it!
 
you probably want to do that using a Tk GUI interface
Anyway it is cleanest to use subroutines and possibly objects here.

use Tk;
my $top=MainWindow->new();
my $frame=$top->Frame()->pack();
my @options=('Filter hits by blocked content',
'Filter hits by passed content',
'Filter hits by IP address',
'Count IP address instances',
'Exit the program');
my $radio=$frame->RadioButton(......
[I can fill in the rest if you are interested]


Anyway to come back to your code

You should not have a loop the way you are doing it:
Just

if(choice==1){... deal with case 1}
elsif(choice==2){deal with case 2}
...
else{exit;}

goto INPUT;


is all you need.

good luck, svar



&MainLoop();

 
can't you set a 'flag' to indicate to the script that the user has already seen the 'Hello' greeting - this could be checked before prompting the user

Duncan
 
The remaining unanswered question is that carg1 wants to see a sixth entry added to the block of choices after the user has initially seen hello and made one choice. I imagine I could put together two completely separate sets of if statements and choice blocks, but that all seems too unwieldy to me. How do you do what he is asking in a streamlined and elegant manner - so to speak?
 
It does come out to be some rather ungainly code. Just to make things clear, I don't want it to generate the sixth option. I'm just doing it in a subroutine:
Code:
sub input2{
my $Choices2;
my $Picknumber2;

INPUT2: $Choices2 = <<'OPTIONS2';
Hey again! What would you like to do next?

1) Open another file for processing
2) Filter hits by blocked content
3) Filter hits by passed content
4) Filter hits by IP address
5) Count IP address instances
6) Exit the program

OPTIONS2

print &quot;\n\n$Choices&quot;;
print &quot;Your choice: &quot;;
$PickNumber = <STDIN>;
chomp($PickNumber2);


if($PickNumber2 == 1) {
	open(INDB, $DB) or die &quot;The database $DB could not be found.\n&quot;;
if($PickNumber2 == 2) {
	read86();
	goto INPUT2;
}
	elsif($PickNumber2 == 3) {
		print &quot;\n\nCan't filter hits by passed content yet.\n&quot;;
		goto INPUT2;
	}
		elsif($PickNumber2 == 4) {
			print &quot;\n\nCan't filter hits by IP address yet.\n&quot;;
			goto INPUT2;
		}
			elsif($PickNumber2 == 5) {
				print &quot;\n\nCan't count IP address instances yet.\n&quot;;
				goto INPUT2;
			}
				elsif($PickNumber2 == 6) {
					print &quot;It's been a pleasure, see ya next time!\n&quot;;
					exit;
				}
}
}
I have attempted it in a loopless manner, but it just ends the program once it runs through the choice picked by the user. I'm inexperienced at Perl, but as far as I can tell, the subroutines are the most streamlined option, but I'm definitely open to suggestions! Maybe I should just uncomplicate things and provide an &quot;Open another file&quot; option in the first OPTIONS block. The other way is more work, but I want to accomplish it for the practice.
 
Here you go mate, this should take care of your troubles for you.

print &quot;\n\nPlease enter the name of the file to load: &quot;;
$DB = <STDIN>;
chomp($DB);
$i = 0;

$string1 = &quot;Hello! What would you like to do with the file?\n\n&quot;.
&quot;1) Filter hits by blocked content\n&quot;.
&quot;2) Filter hits by passed content\n&quot;.
&quot;3) Filter hits by IP address\n&quot;.
&quot;4) Count IP address instances\n&quot;.
&quot;5) Exit the program\n&quot;;

$string2 = &quot;Hey again! What would you like to do next?\n\n&quot;.
&quot;1) Filter hits by blocked content\n&quot;.
&quot;2) Filter hits by passed content\n&quot;.
&quot;3) Filter hits by IP address\n&quot;.
&quot;4) Count IP address instances\n&quot;.
&quot;5) Open another file for processing\n&quot;.
&quot;6) Exit the program\n&quot;;

INPUT:
if ($i==0) {
print $string1;
}
elsif ($i==1) {
print $string2;
}

print &quot;Your choice: &quot;;
$PickNumber = <STDIN>;
chomp($PickNumber);

while (($i==0 && $PickNumber!=5) || ($i==1 && $PickNumber!=6)) {

if($PickNumber == 1) {
print &quot;\n\nCan't filter hits by blocked content yet.\n&quot;;
$i = 1;
goto INPUT;
}
elsif($PickNumber == 2) {
print &quot;\n\nCan't filter hits by passed content yet.\n&quot;;
$i = 1;
goto INPUT;
}
elsif($PickNumber == 3) {
print &quot;\n\nCan't filter hits by IP address yet.\n&quot;;
$i = 1;
goto INPUT;
}
elsif($PickNumber == 4) {
print &quot;\n\nCan't count IP address instances yet.\n&quot;;
$i = 1;
goto INPUT;
}
elsif($i == 0 && $PickNumber == 5) {
print &quot;It's been a pleasure, see ya next time!\n&quot;;
die;
}
elsif($i == 1 && $PickNumber == 5) {
print &quot;\n\nPlease enter the name of the file to load: &quot;;
$DB = <STDIN>;
chomp($DB);
$i = 0;
goto INPUT;
}
elsif($i == 1 && $PickNumber == 6) {
print &quot;It's been a pleasure, see ya next time!\n&quot;;
die;
}
else {
print &quot;Invalid entry. Please try again.\n&quot;;
goto INPUT;
}
}
 
I hit submit before I got to say everything I wanted to. The reason your second script (INPUT2) was dying without looping is that you were assinging user input <stdin> to $PickNumber, but you were using your if/else statements to look for $PickNumber2. Since $PickNumber2 is unitialized (zero) none of the criteria were matched, goto was not called, and the script died.

I fixed that issue for you and tried to implement all the criteria you had specified for your script. The script will run both a &quot;hello&quot; block and a &quot;hey again&quot; block without any cumbersome calls to subroutines or unwieldy duplication of code.

Hope you like it.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top