Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

better way to do N to 1 string mapping

by tanyeun (Sexton)
on Nov 14, 2011 at 06:53 UTC ( #937875=perlquestion: print w/replies, xml ) Need Help??

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

howdy Monks :D

I use a hash to implement this mapping
%category = ( "restaurant A" => "Dine Out", "restaurant B" => "Dine Out", "Walmart" => "Grocery", "HEB" => "Grocery", "Target" => "Grocery" "Movies" => "Fun", "Park" => "Fun", "Shopping" => "Fun" );

so whenever a given $string
%category{$string} will tell you it's category

This looks ugly since the category will grow larger and larger
and it will duplicates multiple lines with the same category
(for example "Grocery" takes 3 lines)

is there a better way to do this?

something like
"restaurant A", "restaurant b" => "Dine Out"
"Walmart", "HEB", "Target" => "Grocery"
"Movies", "Park", "Shopping" => "Fun"

best,

David

Replies are listed 'Best First'.
Re: better way to do N to 1 string mapping
by Corion (Patriarch) on Nov 14, 2011 at 08:07 UTC

    You can turn your mapping on its head, by specifying it like this:

    my %store_for_category = ( "Dine Out" => ['restaurant A', 'restaurant B'], "Grocery" => ['Walmart', 'HEB', 'Target'], ... );

    You can convert from the category to the store by:

    my %category_for_store; for my $category (keys %store_for_category) { for my $store (@{ $store_for_category{ $category }}) { warn "Duplicate category for store '$store': $category_for_sto +re{ $store }, $category" if $category_for_store{ $store }; $category_for_store{ $store } = $category; }; });

    Usually, I end up wanting both mappings anyway, from "store" to "category" and to list all stores for a given category. I also tend to want to put a store into more than one category. I end up stuffing the pairs into an SQL database (like DBD::SQLite), but depending on where you keep the rest of the data, that may or may not be suitable for you.

Re: better way to do N to 1 string mapping
by anneli (Pilgrim) on Nov 14, 2011 at 07:01 UTC

    But what's wrong with %category growing larger? If you definitely want to look items up by item, this is pretty much as good as it gets! It's not duplication; it's what you want!

Re: better way to do N to 1 string mapping
by jethro (Monsignor) on Nov 14, 2011 at 10:42 UTC

    If you are concerned that your perl script will get unwieldy because of this long list, you have many options:

    1) Put that list into a second script file, add "1;" as the last line and use require() to load it

    2) Put that data into a config file and use a module like Config::Any, Config::JSON, Config::Auto (lots more available) to read that file

    3) Use a database (if your data exceeds a few 1000 items)

    4) Use a format of your own

    Note that the resultant file handling, i.e. locating the config or data file and error handling, will add some lines of code so you should think about whether the smaller script file is worth the added complexity. But if you envision changing the data a lot, a config file might really be a good idea

Re: better way to do N to 1 string mapping
by mrguy123 (Hermit) on Nov 14, 2011 at 11:52 UTC
    Hi David, like my fellow monks said there is probably no easy way to make this hash smaller.
    However, if you are worried the hash might become huge, and it is a hash you might need to use several times, a nice option is to use storable.
    That way, you can load very big hashes in a very short time.
    Good Luck
    Guy

    Real programmers don't comment their code. It was hard to write, it should be hard to understand and even harder to modify.
Re: better way to do N to 1 string mapping
by JavaFan (Canon) on Nov 14, 2011 at 09:20 UTC
    is there a better way to do this?
    No.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-06-25 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?