develooper Front page | perl.beginners | Postings from February 2002

loopy loop

Thread Next
From:
terri harrison
Date:
February 24, 2002 07:33
Subject:
loopy loop
Message ID:
F241tzcbLDiKBsuZ2K4000077aa@hotmail.com
This script loops fine but the search only works the first time - nothing 
happens for a second search.  Any idea why?

Terri

#!/usr/bin/perl -w
use strict;

my $exit = 1;
my $bigmatch = 0;
my %rec = ();
my $search = '';

sub read_addr {
    my %curr = ();
    my $curr = '';
    my $key = '';
    my $val = '';

    while (<>) {
	chomp;
	if ($_ ne '***') {
	    ($key, $val) = split(/: /,$_,2);
	    $curr{$key} = $val;
	}
	else { last; }
    }
    return %curr;
}

sub perform_search {
    my ($str, %rec) = @_;
    my $matched = 0;
    my $i = 0;
    my $thing = '';

    my @things = $str =~ /("[^"]+"|\S+)/g;

    while ($i <= $#things) {
	$thing = $things[$i];

	if (!$matched) {
	    $matched = &isitthere($thing, %rec);
	    $i++;
	}
	else { $i++; }
    }

    if ($matched) {
        $bigmatch = 1;
	print_addr(%rec);
    }
}

sub perform_search2 {
    my ($str, %rec) = @_;
    my $matched = 0;
    my $i = 0;
    my $thing = '';

    my @things = $str =~ /LIS ("[^"]+"|\S+)/g;

    while ($i <= $#things) {
	$thing = $things[$i];

	if (!$matched) {
	    $matched = &isitthere($thing, %rec);
	    $i++;
	}
	else { $i++; }
    }

    if ($matched) {
        $bigmatch = 1;
	print_addr(%rec);
    }
}

sub isitthere {
    my ($pat, %rec) = @_;
    foreach my $line (values %rec) {
	if ($line =~ /$pat/) {
	    return 1;
	}
    }
    return 0;
}

sub print_addr {
    my %record = @_;
    print "\n*********************************\n";
    foreach my $key (qw(ID Reference Title Instructor Credit Days Time 
Location)) {
	if (defined($record{$key})) {
	    print "$key: $record{$key}\n";
	}
    }
}

my $in = '';
while ($exit) {
	print "\n1. Search by ID\n";
	print "2. Search by Title\n";
	print "3. Search by Instructor\n";
	print "4. Quit\n\n";
	print "Choose a number: ";

	chomp($in = <STDIN>);


	if ($in eq '1') {
	my $id = '';
	print "Enter an ID: ";
	chomp ($id = <STDIN>);

	my %rec = ();
	while () {
    	%rec = &read_addr();
    	if (%rec) {
	&perform_search2($id, %rec);
    	} else {
	if (!$bigmatch) {
	    print "Nothing found.\n";
	} else { print "*********************************\n"; }
	last;
    }
}
} if ($in eq '2') {
	my $title = '';
	print "Enter a title: ";
	chomp($title = <STDIN>);

	my %rec = ();
	while () {
    	%rec = &read_addr();
    	if (%rec) {
	&perform_search($title, %rec);
    	} else {
	if (!$bigmatch) {
	    print "Nothing found.\n";
	} else { print "*********************************\n"; }
	last;
    }
}
} if ($in eq '3') {
	my $teach = '';
	print "Enter an Instructor: ";
	chomp($teach = <STDIN>);

	my %rec = ();
	while () {
    	%rec = &read_addr();
    	if (%rec) {
	&perform_search($teach, %rec);
    	} else {
	if (!$bigmatch) {
	    print "Nothing found.\n";
	} else { print "*********************************\n"; }
	last;
    }
}
} if ($in eq '4') {
	$exit = 0;
	}

}




_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About