in reply to Possible for Map to create Hash of Hash?

To create a hash that will allow me to determine if a store has a product

You can use different approach: create a hash $h{$store, $product}=1;

See Multi-dimensional-array-emulation in perldoc
  • Comment on Re: Possible for Map to create Hash of Hash?

Replies are listed 'Best First'.
Re^2: Possible for Map to create Hash of Hash?
by Anonymous Monk on May 11, 2013 at 14:14 UTC
    What's the point of splitting $store and $product only to join them again?

      It's joined again with "\034" (by default)

      You will be able to access this hash from other code using $h{$store,$product}. It's easier to write than $h{"$store $product"}

      If, later, you decide that store can have spaces in its name, you will rewrite your first split code, but you won't have to rewrite $h{$store,$product} in other places.

      Anyway, you can ask topic starter why he need split, while he just needs to

      to determine if a store has a product
        > Anyway, you can ask topic starter why he need split, while he just needs to

        > > to determine if a store has a product

        With the nested approach he can also use things like  keys %{$hash{$store}} to list all products.

        The delimiter approach is a nice idea, but is neither shorter to write nor more efficient.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        .. Anyway, you can ask topic starter why he need split, while he just needs to

        I'm sorry, but you've missed my point :) I am questioning the wisdom of your suggestion for an alternative approach.

        Since OP already decided on a hash-of-hashes, why switch back to a simple hash? Or hash-of-array-?emulation?

        Why throw away the benefits of easy lookup?

        There has to be some benefit to doing it differently; and few drawbacks

Re^2: Possible for Map to create Hash of Hash?
by FloydATC (Deacon) on May 13, 2013 at 16:08 UTC
    $h{$store, $product}=1;
    This is flaky to say the least. I assume you meant
    $h{"$store,$product"}=1;
    so atleast it'd be possible to pick them apart later on.

    Even so, it will only be a matter of time until a store pops up with a comma in its name and you'll have to start rethinking the whole thing. Maybe try two or three different delimiters before rewriting to use a better suited data structure.

    Like, say, a hash of hashes ;-)

    -- Time flies when you don't know what you're doing
      I assume you meant $h{"$store,$product"}=1;
      No. Re-read my posting. Also, click the link in my posting and read perldoc article.