in reply to Hashes

I think something like this is what you want,

use Data::Dumper; my %kid; while (<DATA>) { chomp; my @line = split /,/; $kid{$line[1]}{$line[2]}{$line[3]} = { duration => $line[4], function_name => $line[5] }; } print Dumper(\%kid); __DATA__ 1,2,3,4,100,A 10,20,30,40,200,B 11,21,31,41,300,C 12,22,32,42,400,D 13,23,33,43,500,E 13,23,33,53,600,F 13,23,33,63,700,G 13,23,34,73,800,H 13,23,34,83,900,I 13,24,35,93,1000,J 13,24,36,103,1100,K
That prints the data structure as:
$VAR1 = {
          '22' => {
                    '32' => {
                              '42' => {
                                        'function_name' => 'D',
                                        'duration' => '400'
                                      }
                            }
                  },
          '21' => {
                    '31' => {
                              '41' => {
                                        'function_name' => 'C',
                                        'duration' => '300'
                                      }
                            }
                  },
          '24' => {
                    '35' => {
                              '93' => {
                                        'function_name' => 'J',
                                        'duration' => '1000'
                                      }
                            },
                    '36' => {
                              '103' => {
                                         'function_name' => 'K',
                                         'duration' => '1100'
                                       }
                            }
                  },
          '23' => {
                    '33' => {
                              '63' => {
                                        'function_name' => 'G',
                                        'duration' => '700'
                                      },
                              '53' => {
                                        'function_name' => 'F',
                                        'duration' => '600'
                                      },
                              '43' => {
                                        'function_name' => 'E',
                                        'duration' => '500'
                                      }
                            },
                    '34' => {
                              '83' => {
                                        'function_name' => 'I',
                                        'duration' => '900'
                                      },
                              '73' => {
                                        'function_name' => 'H',
                                        'duration' => '800'
                                      }
                            }
                  },
          '2' => {
                   '3' => {
                            '4' => {
                                     'function_name' => 'A',
                                     'duration' => '100'
                                   }
                          }
                 },
          '20' => {
                    '30' => {
                              '40' => {
                                        'function_name' => 'B',
                                        'duration' => '200'
                                      }
                            }
                  }
        };
Is that what you were after?

Deep hashes like this are not the easiest thing to work with. Care must be taken to check existence of a key at each level while looking things up. From the feel of this sample problem, it's possible that Graph will make a more comfortable representation of the data.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Hashes
by flemi_p (Novice) on Jun 22, 2004 at 13:43 UTC
    Hi Zaxo,

    Many thanks for your time and effort deciphering my post and posting, what on the face of it, looks like an excellent reply. This looks like exactly what I want, I must give it a try. I kinda thought this problem would either be very hard/impossible to solve (very unlikely!)or have a concise elegant solution like you've posted.

    I intend to have a play and may come back with some more questions, if thats ok?

    Thanks
    Pat