Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Hash table

by Ammu (Novice)
on Sep 09, 2012 at 14:34 UTC ( #992589=perlquestion: print w/ replies, xml ) Need Help??
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..

Comment on Hash table
Re: Hash table
by Corion (Pope) 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 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992589]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-11-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (199 votes), past polls