drh has asked for the wisdom of the Perl Monks concerning the following question:

Real newbie I'm afraid, started learning yesterday. I have managed to install all the modules I need to communicate with access.

I can also read from the table in Access I want and print out the data line by line but what I really need to do is store the two fields in a hash for later use.

Sorry there is no code to build on yet but everything I have tried has been no good,

Thanks

DRH

Replies are listed 'Best First'.
Re: Reading data from Access into a Hash
by lachoy (Parson) on May 03, 2001 at 21:09 UTC

    By 'later use' do you mean later in the script or later in an entirely dseparate session?

    In any case, you want something like:

    use strict; use DBI; # # ... connect to DB, execute the SELECT # statement to the statement handle $sth... # # This method is slower than selecting an # arrayref, but it has the benefit of being # easy to understand # 'KEY_FIELD' is the name of your key field, # 'DATA_FIELD' is the name of the data field # you want to save my %records = (); while ( my $row = $sth->fetchrow_hashref ) { $records{ $row->{'KEY_FIELD'} } = $row->{'DATA_FIELD'}; } # You can now do anything you want with # %records and use the information without # dealing with the database. # ...later... print "Records retrieved:\n"; while ( my ( $key, $value ) = each %records ) { print "$key --> $value\n"; }
    Hope this helps, Chris