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

I have a problem searching a flat file database - anyone help 1

Status
Not open for further replies.

SCRUFFYbEAR

IS-IT--Management
Oct 27, 2000
5
GB
Hi,
on a web page a user fills in two fields hits the button and gets a value returned.
in the script the values returned are validated and joined to make the search query - this works okay :-o)
the flat file has two fields per record seperated by a | the first field is the unique identifier the second is the value - when the search occurs it does not find any results even though there is a definite match in the base.
the following is the piece of code that fails, it always prints out the fail part!!
Code:
####You have the search string - now search####

open (FILE, "$database");
while (<FILE>) {			# range over address file
    %rec = &read_addr();
    if (%rec) {		# got a record
	&perform_search($search, %rec);
    } else {		# end of address file, finish up
            print &quot;Content-type: text/html\n\n&quot;;
            print &quot;<html><head><title>Thank you!</title></head>\n&quot;;
            print &quot;<BODY><h1>Thank you!</h1><br>Thanks for your input! \n&quot;;
            print &quot;width....$width\n&quot;;
            print &quot;height....$height\n&quot;;
            print &quot;style....$style\n&quot;;
            print &quot;search....$search\n&quot;;
            print &quot;record....$curr\n&quot;;
            print &quot;not in here. \n&quot;;
	if (!$bigmatch) {
	    &print_error();
	} else { print &quot;*********************\n&quot;; }
	last;		# exit, we're done
    }  
}

# read an item from the file, split it into
# values, and store those values in a hash.  Return that hash.
sub read_addr {
    my %curr = ();		# current record
    my $key = '';		# temp key
    my $val = '';		# temp value

    while (<>) {		# stop if we get to EOF
	chomp;
	if ($_ ne '---') {	# record seperator
	    ($key, $val) = split(/|/,$_,2);
	    $curr{$key} = $val;
	}
	else { last; }
    }
    return %curr;
}
anybody help?

TIA

terry

 


In this line......
Code:
($key, $val) = split(/|/,$_,2);
the pipe, |, is a metacharacter which means 'or' in REGEX speak. You need to escape it like......
Code:
($key, $val) = split(/\|/,$_,2);

'hope this helps.....








keep the rudder amid ship and beware the odd typo
 
Hi,
thanx for the quick response, you were right about the | not being escaped, done that but i get the same result, it basically still fails the test and says that no records were returned, but the records are in the test file!!
any other ideas??
Code:
####You have the search string $search - now search####

open (FILE, &quot;$database&quot;);
while (<FILE>) {			# range over the file
    %rec = &read_addr();
    if (%rec) {		# got a record
	&perform_search($search, %rec);
    } else {		# end of address file, finish up
            print &quot;Content-type: text/html\n\n&quot;;
            print &quot;<html><head><title>Thank you!</title></head>\n&quot;;
            print &quot;<BODY><h1>Thank you!</h1><br>Thanks for your input! \n&quot;;
            print &quot;width....$width\n&quot;;
            print &quot;height....$height\n&quot;;
            print &quot;style....$style\n&quot;;
            print &quot;search....$search\n&quot;;
            print &quot;not in here. \n&quot;;
	if (!$bigmatch) {
	    &print_error();
	} else { print &quot;*********************\n&quot;; }
	last;		# exit, we're done
    }  
}

# read an item from the file, split it into
# values, and store those values in a hash.  Return that hash.
sub read_addr {
    my %curr = ();		# current record
    my $key = '';		# temp key
    my $val = '';		# temp value

    while (<>) {		# stop if we get to EOF
	chomp;
	if ($_ ne '---') {	# record seperator
	    ($key, $val) = split(/\|/,$_,2);


	    $curr{$key} = $val;
	}
	else { last; }
    }
    return %curr;
}

regards

terry
 
terry,

Are you expecting read_addr to loop through FILE? It's actually reading stdin.

Could you write a short description of what you'd like th script to do?

Mike
michael.j.lacey@ntlworld.com
 
Hi Mike,
the idea is that on a web page a user inputs two values, height and width - depending on the page they are on a hidden field contains a code like &quot;STY1-&quot; these variables are passed to the script the height and width are assessed and rounded to the nearest size, the three are then joined together to form $search &quot;STY1-450-600&quot; this works okay.

I then need to look through a data file and find a match in field one of the record and print out field two, the first field will be unique:-
STY1-450-450|33
---
STY1-450-600|35
---
STY1-450-750|36
---
STY1-450-900|40
---
STY1-450-1050|43
---
STY1-450-1200|46
---
STY1-450-1350|49
---

I thought that i had got read_addr to search through the file, can you help put me right?

Regards

Terry
Nature, Beauty, Cornwall.
 
hi terry,

don't think you need th while loop in the main program

try

while(<FILE>)

in read_addr

don't *think* you can return a hash from a function like you're trying to do with read_addr, just call read_addr from your main program without assigning it's return code to %curr

Mike
michael.j.lacey@ntlworld.com
 
Hi Mike,
thanks for the help, but it does exactly the same thing, goes to the else part of the routine and prints the error part. I must be way off track somewhere, when i get back in acouple of hours i will try rewriting that section, any other suggestions gratefully received.
Regards

Terry
Nature, Beauty, Cornwall.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top