Hi Birgit--

I am probably missing something, but the thing I see is that you have
used the my keyword inside your loop in this statement:

> my @db_cols = @cols; # now we set the field names to those of db2
which we
> my ($status2, @hits2) = &query('view'); # sub query does the actual
search

that makes @db_cols, $status2, and @hits2 local to the if{} structure

That means you lose the value of those variables when you go outside
the if{}

demonstrated by this code:
$one = 1;
for (my $i=0;$i<2;$i++) {
 print "i=$i, one = $one\n";
 my $one = 2;
 print "i=$i, one = $one\n";
}
print "i=$i, one = $one\n";

which will give you this:
i=0, one = 1
i=0, one = 2
i=1, one = 1
i=1, one = 2
i=, one = 1

so declare those variables outside the if{} and delete the my's
inside the if{}
-----Original Message-----
From: Birgit Kellner <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: August 10, 2001 8:03 AM
Subject: problems with a for-loop


>I'm stuck with a for-loop and would appreciate any help I can get. I
hope
>the code is somewhat intelligible, as it comes out of a larger
script.
>
>The code serves to print out individual search results for a
flatfile
>database query, called in a cgi-script.
>All matching records are stored in @hits, the humber of hits is
stored in
>$numhits.
>What I want to achieve is to first print out each search result,
culled
>from the file db1.db, and then switch to a second database file
db2.db and
>get all records there for a particular entry in db1.db.
>Example: db1.db contains a record where "lastname" is "Muller", and
I now
>want to get the record from db2.db where "lastname" is also "Muller.
Each
>database file has its own list of database fields, which are stored
in a
>database configuration file (db1.cfg, db2.cfg).
>
># get input from database query
>my ($numhits, $maxhits, @hits) = @_;
># this code block changes the database setup from db1 to db2
># $db_file_$name holds the database file name, @db_cols holds the
field
>names.
>$old_file_name = $db_file_name;
>$db_file_name  = $db_script_path . "/db2.db";
>@old_cols = @db_cols;
>@cols = &get_db_cols('db2.cfg'); # getting the field names for db2
from the
>config-file db2.cfg
>## end changing setup
>for (0 .. $numhits - 1) { # for all matches
> if ($in{'generate'}) { # we do this on the condition that a value
of
>"generate" exists in the query-string
> my ($firstname, $lastname) = &html_record_generate
(&array_to_hash($_,
>@hits));
> # the subroutine html_record_generate prints out the individual
search
>results for
> # the query, matching up elements of @hits with the field names
stored in
>@db_cols.
> # at this point, @db_cols still needs to have the values for db1.
> # $firstname and $lastname are the values on which we want to query
db2
> my @db_cols = @cols; # now we set the field names to those of db2
which we
>have read into @cols above
> # This is clumsy, but reading in the field names inside the for
loop
>doesn't work as it should.
> $in{'keyword'} = "$firstname $lastname"; # this is what we're going
to
>search on in db2
> $in{'bool'} = "and"; # add a search option for searching in db2
> my ($status2, @hits2) = &query('view'); # sub query does the actual
search
> if ($status2 eq "ok") { # if there is a match
> my (%rec2) = &array_to_hash (0, @hits2); # turn the hits into a
hash.
> print |<br>$rec2{'mail'}, $rec2{'phone'}, and so on.<br>|;
> }
> }
> else { # if there's no value of "generate" in the query-string, we
don't
>need any of this and go for a different sub.
> &html_record (&array_to_hash($_, @hits));
> }
>
>Two problems:
>First, the values of $firstname and $lastname change in the course
of the
>for-loop as they should, but the query to db2 keeps returning search
>results only for the first match. Example: In the first loop,
$firstname =
>"Charles" and $lastname = "Miller" are returned from
html_record_generate.
>There's a match for them in db2.db, and it gets returned in @hits2.
In the
>second loop, $firstname = "Sandra" and $lastname = "Kwack" are
returned.
>There's also a match for them in db2.db, but it doesn't get returned
as
>@hits2 - @hits2 stays with good old Charles Miller.
>
>Second, I get the matching record into @hits2, but not into the hash
%rec2.
>I reckon this has got something to do with the "0" I'm passing on to
sub
>array_to_hash, but am not sure what.
>
>I hope this is somewhat intelligible ...
>
>Birgit Kellner
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to