http://qs321.pair.com?node_id=1148839


in reply to Tree Structure Challenge

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; dd( deeef( 'Cobra', 'Fox' ) ); #~ should return #~ ( [qw[ Fox Canine Mammal ]], #~ [qw[ Cobra Snake Reptile ]] #~ ) sub deeef { my( @animals ) = @_; my %catcatdog = ( Mammal => { Bovine => ["Cow", "Bison"], Canine => ["Dog", "Fox", "Wolf"], Equine => ["Horse", "Zebra", "Pony"], }, Reptile => { Bird => ["Pigeon", "Canary", "Owl"], Lizard => ["Salamander", "Chameleon"], Snake => ["Python", "Cobra"], }, ); my %dogcatcat; for my $cat ( keys %catcatdog ){ for my $catcat ( keys %{ $catcatdog{ $cat } } ){ for my $dog( @{ $catcatdog{$cat}{$catcat} } ){ #~ $dogcatcat{$dog}{$cat}{$catcat}++; #~ $dogcatcat{$dog}{$catcat}++; #~ $dogcatcat{$dog}{$cat}++; push @{ $dogcatcat{$dog} }, $catcat, $cat, ; } } } #~ return map {; [ $_, keys %{ $dogcatcat{$_} } ] } @animals; return map {; [ $_, @{ $dogcatcat{$_} } ] } @animals; }

Replies are listed 'Best First'.
Re^2: Tree Structure Challenge
by choroba (Cardinal) on Nov 30, 2015 at 00:00 UTC
    How should I run your solution to get the result for the following?
    'Zoo'->diff( 'Zoo'->new(qw( Dog Fox Wolf )), 'Zoo'->new(qw( Fox )));

    Not saying that I'm getting

    Can't use an undefined value as an ARRAY reference at ./zoo.pl line 40 +.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      How should I run your solution to get the result for the following?

      What should the result look like?

      Not saying that I'm getting ...

      Huh?

      dd( deeef( qw/ Dog Fox Wolf / ) ); __END__ ( ["Dog", "Canine", "Mammal"], ["Fox", "Canine", "Mammal"], ["Wolf", "Canine", "Mammal"], )
        What should the result look like?
        ( [], [ 'Dog', 'Wolf' ] )

        Huh?
        I know how to run your code. It doesn't follow the specification: you haven't implemented a static method.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,