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

Hi

I'm a Perl newbie with the following task: I need to take a very long string and parse the elements of that string. The string will contains multiple occurrences of:

Dog: Beagle

Food: Type 3

Dog: Retriever

Food: table scraps

The string will have a bunch of unneeded text before and after these multiple occurences. After I parse the elements, I want to put them into a data structure. I want to group each 'Dog' - value together, and each 'Food' - value together. I also want to group each 'dog/food' pair together. I will retrieve the this information later based on the Dog value.

I think I want to use a hash of hashes. I've been reading the appropriate parts of 'Programming Perl' (which is great), but I would appreciate a nudge in the right direction, as none of the examples matches my case closely enough for me to see a solution.

Thanks!

tl

  • Comment on Creating data structure from multiple pairs of information

Replies are listed 'Best First'.
Re: Creating data structure from multiple pairs of information
by RazorbladeBidet (Friar) on Feb 17, 2005 at 18:02 UTC
    You really have just one key value pair. The other items you want (all groups and all dogs) are arrays, they have no real relation to one another except their group. The dog/food pairing, however, is very specific.

    Perl provides a way to access these arrays, were you to place the dog/food pairing in a hash. See keys and values. That should take care of your data structuring issue (if I am understanding you correctly).

    For the regular expressions I point you to perlre. If you look through this first then maybe we can get down to some of the specifics for getting that data into a hash. If you've already read it, great! Do you have any cases you've tried or any ideas to go on?

    HTH!
Re: Creating data structure from multiple pairs of information
by Fletch (Bishop) on Feb 17, 2005 at 18:01 UTC

    Smells like homework. Or possibly that's just wet dog fur. Show what code you've tried so far and you're more likely to get something useful.

Re: Creating data structure from multiple pairs of information
by jcoxen (Deacon) on Feb 17, 2005 at 18:07 UTC
    Assuming your're not going to have repeats of dog types, you could arrange your hash as :

    %dog_hash (
    "Beagle" => "Type 3",
    "Retriever" => "table scraps"
    );

    Jack

      Why does it matter if your dog repeats?
      if($dog_hash{$dog_type}){ print "dog [$dog_type] already in hash\n"; }else{ $dog_hash{$dog_type} = $food_type; }
      I think that RazorbladeBidet's referral is probably the way to go if you really want to know how to hash well.
        or

        push @{$dog{$dog_type}}, $food_type;

        if more than one food type is possible.
        (That is, a hash of array references)
        --------------
        It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs