How to store an inventory using hashtables?

Posted by Harm De Weirdt on Stack Overflow See other posts from Stack Overflow or by Harm De Weirdt
Published on 2010-03-20T12:34:01Z Indexed on 2010/03/20 12:41 UTC
Read the original article Hit count: 276

Filed under:
|

Hello everyone. For an assignment in collego we have to make a script in Perl that allows us to manage an inventory for an e-store. (The example given was Amazon) Users can make orders in a fully text-based environment and the inventory must be updated when an order is completed.

Every item in the inventory has 3 to 4 attributes: a product code, a title, a price and for some an amount (MP3's for example do not have this attribute)

Since this is my first encounter with Perl, i don't really know how to start. My main problem is how i should "implement" the inventory in the program. One of the functions of the program is searching trough the titles. Another is to make an order, where the user should give a product code.

My first idea was a hashtable with the productcode as key. But if i wanted to search in the titles that could be a problem because of this: the hashkey would be something like DVD-123, the information belonging to that key could be "The Green Mask 12" (without the ") where the 12 indicates how many of this DVD are currently in stock. So i'd have to find a way to ignore the 12 in the end.

Another solution was to use the title as Hashkey, but that would prove cumbersome too I think.

Is there a way to make a hashtable with 2 key's, and when I give only one it returns an array with the other values? (Including the other key and the other information) That way I could use another key depending on what info I need from my inventory.

We have to read the default inventory from a txt file looking like this: MP3-72|Lady Gaga - Kiss and Run (Fear of Commitment Monster)|0.99 CD-400|Kings of Leon - Only By The Night|14.50|2 MP3-401|Kings of Leon - Closer|0.85 DVD-144|Live Free or Die Hard|14.99|2 SOFT-864|Windows Vista|49.95

Any help would be appreciated very much :)

PS: I am sorry for my bad grammar, English isn't my native language.

© Stack Overflow or respective owner

Related posts about perl

Related posts about hashtable