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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user options STDIN 1

Status
Not open for further replies.

moonhead

Technical User
Apr 24, 2002
11
GB
Hi All
the code below is doing a search on a file and outputting the results to screen.
i would like to give the user the option of doing another search. as you can see i have an if statement setup but i can't call the while loop.
how do i do it?

open (FILE, "files/comp.log");


$msisdn=<STDIN>;

chomp $msisdn;


print &quot;searching...\n&quot;;
sleep 2;

print &quot;searched OUTPUT file:\n&quot;;

while(<FILE>)
{
print if /$msisdn/;
$count++;
$count ||= &quot;No&quot;;
}

print &quot;\n$count records searched...\n&quot;;






print &quot;do another search? Y\,N\n&quot;;
if (<> =~/^[y]/i)
{

}
else
{
exit;
}


close <FILE>;

thanks MoonHead(&quot;_&quot;)

 
Try wrapping the search piece in a subroutine, then just call it in the if statement.

Something to the effect of:

Code:
sub searchrecs
{
    open (FILE, &quot;files/comp.log&quot;);

    $msisdn=<STDIN>;
    $count = 0;
    chomp $msisdn;

    print &quot;searching...\n&quot;;
    sleep 2;

    print &quot;searched OUTPUT file:\n&quot;;

    while(<FILE>)
        {
            print if /$msisdn/;
            $count++;
            $count ||= &quot;No&quot;;
        }
    
    print &quot;\n$count records searched...\n&quot;;

    close <FILE>;
}

#Add your code to start first search here

print &quot;do another search? Y\,N\n&quot;;
if (<> =~/^[y]/i)
{
    &searchrecs();
}
else
{
    exit;
}

- Rieekan
 
Ignore that last post. I realized after I posted it that it only covered doing a total of two searches. Try this instead:

Code:
sub searchrecs
{
    open (FILE, &quot;files/comp.log&quot;);

    $msisdn=<STDIN>;
    $count = 0;
    chomp $msisdn;

    print &quot;searching...\n&quot;;
    sleep 2;

    print &quot;searched OUTPUT file:\n&quot;;

    while(<FILE>)
        {
            print if /$msisdn/;
            $count++;
            $count ||= &quot;No&quot;;
        }
    
    print &quot;\n$count records searched...\n&quot;;

    close <FILE>;
    doanother();
}

sub doanother
{
    print &quot;do another search? Y\,N\n&quot;;
    if (<> =~/^[y]/i)
    {
        &searchrecs();
    }
    else
    {
        exit;
    }
}

#add code that happens before the first search here

&searchrecs();

- Rieekan
 
Rieekan

i have learned a lot from what you have given
in the past i have always had trouble with loops
this has helped a great deal
Thanks
moonhead(&quot;_&quot;)
 
You could also take this approach:

my $input_file = &quot;files/comp.log&quot;;
open (FILE, &quot;<$input_file&quot;) || die &quot;Can't open $input_file for reading!&quot;;

while(1) {
while (<FIILE>) {
print if /$msisdn/;
}

print &quot;do another search? Y\,N\n&quot;;
my $answer = <>;
if ($answer eq &quot;N&quot; || $answer eq &quot;n&quot;) {
last; ### break out of while(1) loop
}

} ### end while

That way, your outer loop will continue on until the user answers &quot;N&quot; to question &quot;Do another search?&quot;.

And, now that I'm thinking about it, if the user answers &quot;Y&quot; that he *would* like to do another search, I think you'll need to close(FILE) and reopen it for reading - I don't think it will work unless you do that - you need to somehow cause the filehandle to reposition at the start of the file, and although there's probably a quick way to do that, the only way I know how to do it is to close and re-open the file.

The other way to accomplish this is at the start, read the whole file into an array(each line will be read into an array element), and then in your while loop, process the array, like this:

my $input_file = &quot;files/comp.log&quot;;
open (FILE, &quot;<$input_file&quot;) || die &quot;Can't open $input_file for reading!&quot;;

my @lines = <FILE>;
close(FILE);

while(1) {
foreach $line (@lines) {
print if /$msisdn/;
}

print &quot;do another search? Y\,N\n&quot;;
my $answer = <>;
if ($answer eq &quot;N&quot; || $answer eq &quot;n&quot;) {
last; ### break out of while(1) loop
}

} ### end while

Using this approach you don't need to worry about closing and re-opening the file for reading each time. ***BUT, this approach reads your whole file into memory, so if you have a LARGE file, and memory is an issue, you may NOT want to use this approach.

HTH. Hardy Merrill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top