http://www.perlmonks.org?node_id=992589

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

Do hashtables in Perl always have keys??I am asked to create a hashtable from a database.But there is no unique values so that cant make any column as key..A lil confused after hearing this requirement.Also i was asked to iterate through each rows in this hash table.Any help is really appreciable..

Replies are listed 'Best First'.
Re: Hash table
by moritz (Cardinal) on Sep 09, 2012 at 15:04 UTC
    Do hashtables in Perl always have keys?

    An empty hash doesn't have any keys, but it's not the most useful case.

    I am asked to create a hashtable from a database.But there is no unique values so that cant make any column as key.

    I guess you haven't been asked to make a hash table just for the fun of it, but because somebody needs it. That somebody can tell you what the keys should be.

    Note that even if there is no unique column in your database, there might a comination of columns that is unique, which you could then use as keys for the hash table (for example delimited by the zero-byte "\x0").

    Or the user of the hash table might expect multiple values to be grouped as an array, or simply the first or last of the values.

    A lil confused after hearing this requirement.

    Then asked for clarification. But don't ask us, ask the one who made the requirements.

Re: Hash table
by Corion (Patriarch) on Sep 09, 2012 at 14:59 UTC

    From your other questions, it seems to me as if you are confusing the keys of a hash, and the (primary) keys in a database. These two are different concepts.

    If you are starting out with Perl, perlintro maybe is a good start. Also, consider the each keyword.

    But thinking more about your questions, I think that you don't want a hash at all, you want to fetch (by using ->selectall_arrayref) a list of rows. These rows can be hashes again, with one key per column.

Re: Hash table
by rpnoble419 (Pilgrim) on Sep 09, 2012 at 16:53 UTC
    I agree with moritz. But having had to produce what you are asking for, I use a counter to create the hash key when reading through your database return. You can create the unique key as part of your select by creating a row counter, but understand that the row counter will change based on the result of your search. You can also add a counter to the hash as part of your assignment to the hash.

    Code to create a row counter in MySQL

    SET @key := 0; select @key := @key + 1, DataElementToSelect from Database.Table;
Re: Hash table
by chip (Curate) on Sep 09, 2012 at 20:44 UTC
    It always has keys, but it need not have (interesting) values.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Hash table
by Jim (Curate) on Sep 09, 2012 at 21:14 UTC
    I am asked to create a hashtable from a database. … Also i was asked to iterate through each rows in this hash table.

    If I were a Perl programming teacher, I'd use this same problem for a homework assignment.