Hello Birgit,

Thursday, August 23, 2001, Birgit Kellner <[EMAIL PROTECTED]> wrote:

BK> my ($db_key, %rec);
BK> # %orderhash contains numeric keys and values, like ('43' => '5', '20' => 
BK> '17'); I want to call the sub get_record for each of these keys.
BK> # NOTE: this works perfectly, with the same syntax, in other parts of the 
BK> same file.
BK> # might the problem be that I'm embedding the call to the sub in a foreach 
BK> loop here?

BK> foreach (keys %orderhash) {
BK>                 $db_key = $_;
BK>                 print "Database key: $db_key"; #this works fine, so $db_key HAS a 
numeric 
BK> value
BK>                 %rec = &get_record($db_key); # here's the problem: %rec doesn't 
get 
BK> returned
BK>                 print "Number: $rec{'number'}"; # ouch, no value.
BK>                 }

BK> # this is the called sub, contained in another file that we're requiring.
BK> # other file also runs under use strict - no errors reported. all variables 
BK> not declared in the sub are declared globally.
BK> # I won't give all explanations about what this does just yet - hoping that 
BK> this is a simple logic error which I'm too blind to see ...

BK> sub get_record {
BK> # --------------------------------------------------------
BK> # Given an ID as input, get_record returns a hash of the
BK> # requested record or undefined if not found.

BK>         my ($key, $found, $line, @data, $field, $restricted, $i, %rec);
BK>         $key = $_[0];   
BK>         $found = 0;
BK>         ($restricted = 1) if ($auth_modify_own and !$per_admin);

BK>         open (DB, "<$db_file_name") or &cgierr("error in get_records. unable to 
BK> open db file: $db_file_name.\nReason: $!");
BK>         if ($db_use_flock) { flock(DB, 1); }    
BK>         LINE: while (<DB>) {
BK>                 (/^#/)      and next LINE;
BK>                 (/^\s*$/)   and next LINE;
BK>                 $line = $_;     chomp ($line);          
BK>                 @data = &split_decode($line);
BK>                 next LINE if ($restricted and ($db_userid ne 
$data[$auth_user_field]));
BK>                 if ($data[$db_key_pos] eq $key) {
BK>                         $found = 1;
BK>                         for ($i = 0; $i <= $#db_cols; $i++) {  # Map the array 
columns to a hash.

Unfortunatelly, my telepathic abilities still seriously broken :)
please put
print "key = $db_cols[$i], value = $data[$i]";
here and show us output.
BK>                                 $rec{$db_cols[$i]} = $data[$i];
BK>                         }
BK>                         last LINE;
BK>                 }
BK>         }
BK>         close DB;       
BK>         $found ?
BK>                 (return %rec) :
BK>                 (return undef);
BK> }

Birgit, please, provide as more information as possible with your question, not only
piece of code. it's very hard to understand, what can be wrong.

Best wishes,
 Maxim                            mailto:[EMAIL PROTECTED]



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

Reply via email to