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

Gurus: I'm trying to put data into a hash using my first field as the key. I'm still not familiar enough with hashes to add it. I know the below is wrong, but I think I'm close.
my %hash; while ($connection->FetchRow()) { my %dataRow = $connection->DataHash(); %hash($dataRow{Client_ID}); };
Thank you. D-Rod

Replies are listed 'Best First'.
Re: Win32::ODBC results in hash
by sierpinski (Chaplain) on Mar 22, 2010 at 19:11 UTC
    What is Client_ID? The key? If it's a variable, it's missing its prefix ($, @, etc).

    Hashes are assigned data in many ways:
    %hash{$key} = $value; or %hash = ( $key => $value); another one: %hash = ('$key1','$value1','$key2','$value2');
    Notice that the first one uses curly braces {}, but the second and third ones use standard parens.

    Update: fixed typos... long day!
      Client_ID is the field I want to use as the key.
        In that case you'd want to do something like this:
        %hash{"Client_ID"} = $dataRow;
        This will store the value of $dataRow into %hash, with the index (key) being the string "Client_ID".

        I highly recommend getting Data::Dumper from CPAN so you can dump your data structure to a scalar and print it. It makes it very easy to see when you've done something wrong, and for being new to hashes, I think it's a great tool. I use it all the time.
Re: Win32::ODBC results in hash
by toolic (Bishop) on Mar 22, 2010 at 19:27 UTC
      I don't why, but I get syntax error. I receive the follow syntax error: "%hash("
      while ($connection->FetchRow()) { %dataRow = $connection->DataHash(); %hash( $key => $dataRow{Client_ID} ); };
        I finally got it to work. Thanks for your help.
        my %hash = (); while ($connection->FetchRow()) { my %dataRow = $connection->DataHash(); $hash{ $dataRow{Client_ID} } = $dataRow{Contact_Information}; };