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!

This should work

Status
Not open for further replies.

mrchaos70

Technical User
Apr 15, 2002
7
US
Ok this should work, Im teaching my self Perl and I can get chunks of code to work, BUT when I put it together it craps out on me. Can somebody tell me what is worng with this?

The problem is in the sub showOneRecord
When I rund that chunk of code by its self it works just right, but the second I add that code to the sub then it takes a crap on me. What am I missing?

CODE FOLLOWS

print "\nThis program writes to a flie,\n";
print "\nPress any key to continue...";
$anychar=<stdin>;



for(;;){

print&quot;\n\t1. Add Record&quot;;

print&quot;\n\t5. View All Records.&quot;;


print &quot;\n\tWhat action do you want to perform?\n\t&quot;;
$answer = <STDIN>;
chomp($answer);
if($answer =~ /^1/i){
#add
print &quot;\n\tOption 1.\n&quot;;
enterData();
last;
}
elsif($answer =~ /^5/i){
#view all
print &quot;\n\tOption 5.\n&quot;;
showAll();
last;
}

else{
print &quot;\n\tMust select a valid option.\n&quot;;
next;
}


}#end for


sub enterData {

#---------------Inputs user data into Vars



print &quot;\nBuilding you into the data base...\n &quot;;

print &quot;\nEnter Your Name: &quot;;
$namedata = <stdin>;
chomp($namedata);

print &quot;\nEneter your Email address: &quot;;
$emaildata =<stdin>;
chomp($emaildata);

print &quot;\nEnter your Street Address: &quot;;
$streetadd = <stdin>;
chomp($streetadd);

print &quot;\nEnter your city: &quot;;
$city = <stdin>;
chomp ($city);


print &quot;\nEnter your State: &quot;;
$state= <stdin>;
chomp($state);


print &quot;\nEnter your zip code: &quot;;
$zip = <stdin>;
chomp($zip);

}
#---------------------list all records


sub showOneRecord{

print &quot;\n\nNow printing a list of all records&quot;;
print &quot;\n\tPress any key to print&quot;;

$anychar=<stdin>;

open(RFH,&quot;<basicdata.txt&quot;)||die(&quot;File could not be opend: $!&quot;);
$count=0;
while(my $line= <RFH>)
{
print &quot;$line\n&quot;;
$count++;
if($count%15==0)
{
print &quot;Push any key to continue &quot;;
$count=0;


}
}
close(RFH);

$anychar=<stdin>;

}

 
sub showOneRecord{
print &quot;\n\nNow printing a list of all records&quot;;
print &quot;\n\tPress any key to print&quot;;
$anychar=<stdin>;
open(RFH,&quot;<basicdata.txt&quot;)||die(&quot;File could not be opend: $!&quot;);
$count=0;
while(my $line= <RFH>)
{
print &quot;$line\n&quot;;
$count++;
if($count%15==0)
{
print &quot;Push any key to continue &quot;;
$count=0;
}
}
close(RFH);
$anychar=<stdin>;
}

tips:
twice the line $anychar=<stdin>; ???
if($count%15==0) ????? if($count == 15) no?

or try to be more explict in the loop
here usig foreach istead of while
@storer = <RFH>
foreach $line (@storer) {
print &quot;\n$line&quot;;
if ($count == 15) {
print &quot;\nhit Enter to continu&quot;;
$thefakepauservar = <stdin>;
$count = 0;
}
count++;
}

place the count++ the last statement of your loop
in your example u say
$count = 0
#looping....
$count++ # now is 1
#check count

see u never checked if count was 0


someone knowledge ends were
someone else knowledge starts
 
Thanks, like I said im still kind of new to perl, I seem to be unable to get you code to work at all...is there something you are leaving out that you asumed I should know?
 
hi MrChaos

Try it like this, you don't need to specify STDIN when you want input from the user and your menu structure was a bit iffy.

Mike

$|=1;

for(;;){

print&quot;\n\t1. Add Record&quot;;
print&quot;\n\t5. View All Records.&quot;;
print&quot;\n\tQuit.&quot;;
print &quot;\n\tWhat action do you want to perform?\n\t&quot;;
$answer = <>;
chomp($answer);
if($answer =~ /1/){
#add
print &quot;\n\tOption 1.\n&quot;;
enterData();
}elsif($answer =~ /5/){
#view all
print &quot;\n\tOption 5.\n&quot;;
showAll();
}elsif($answer =~ /[Qq]/){
#view all
exit 0;
}else{
print &quot;$answer\n&quot;;
print &quot;\n\tMust select a valid option.\n&quot;;
next;
}
}#end for


sub enterData {

#---------------Inputs user data into Vars



print &quot;\nBuilding you into the data base...\n &quot;;

print &quot;\nEnter Your Name: &quot;;
$namedata = <>;
chomp($namedata);

print &quot;\nEneter your Email address: &quot;;
$emaildata =<>;
chomp($emaildata);

print &quot;\nEnter your Street Address: &quot;;
$streetadd = <>;
chomp($streetadd);

print &quot;\nEnter your city: &quot;;
$city = <>;
chomp ($city);


print &quot;\nEnter your State: &quot;;
$state= <>;
chomp($state);


print &quot;\nEnter your zip code: &quot;;
$zip = <stdin>;
chomp($zip);
open(RFH,&quot;>>basicdata.txt&quot;)||die(&quot;File could not be opend: $!&quot;);
print RFH &quot;$namedata $emaildata $streetadd $city $state $zip\n&quot;;
close(RFH);

}
#---------------------list all records


sub showAll{

print &quot;\n\nNow printing a list of all records&quot;;
print &quot;\n\tPress any key to print&quot;;

$anychar=<>;

open(RFH,&quot;<basicdata.txt&quot;)||die(&quot;File could not be opend: $!&quot;);
$count=0;
while(my $line= <RFH>)
{
print &quot;$line\n&quot;;
$count++;
if($count%15==0){
print &quot;Push any key to continue &quot;;
$count=0;
}
}
close(RFH);

$anychar=<>;

}
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top