> This script loops fine but the search only works the > first time - nothing happens for a second search. > Any idea why?
> #!/usr/bin/perl -w > use strict; > > my $exit = 1; > my $bigmatch = 0; > my %rec = (); > my $search = ''; These should be constant, your script will break if they change during execution. (unless that's what you intented). > sub read_addr { > my %curr = (); > my $curr = ''; > my $key = ''; > my $val = ''; Ew, what's this doing here? $curr never used, and assignments to "" not requrired. > while (<>) { > chomp; > if ($_ ne '***') { > ($key, $val) = split(/: /,$_,2); > $curr{$key} = $val; my ($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); > } > } Do I see double? Above and below look similar. > 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 is_it_there { my ($pat, %rec) = @_; foreach (values %rec) { return 1 if /$pat/; } 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 = ''; The above is probably misplaced. > 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; > } > > } Fix some of the duplicate code/indenting/style and I'll have a proper look. I reckon you can half the number of lines without reducing clarity. The problem you have is probably to do with "my $in = '';", but maybe not. Even with strict, you don't need to bother predeclaring variables at the top of subroutines. Putting things in the right place makes it much easier to read (and if I remember right using 'my' in the inner loop isn't that much slower (if not faster)). Take care, and feel free to demand that I examine the code a little more carefully. :) Jonathan Paton __________________________________________________ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]