By confining the structure to hashes and avoiding arrays you don't have to worry about duplicates and, by always pre-assigning an anoymous hash, you can also dispense with the bother of turning a scalar value into a hash or an array. I think this is a little simpler and clearer than muba's solution if the structure produced is what you are after.

use strict; use warnings; use Data::Dumper; open my $univFH, q{<}, \ <<EOD or die qq{open: < HEREDOC: $!\n}; abc 123 abc 456 abc 789 xyz 456 EOD my %univ; while ( <$univFH> ) { my( $lev2Key, $lev1Key ) = split; $univ{ $lev1Key }->{ $lev2Key } = {}; } close $univFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ %univ ], [ qw{ *univ } ] ); open my $valuesFH, q{<}, \ <<EOD or die qq{open: < HEREDOC: $!\n}; xxx ==> 456->abc xxx ==> 789->abc yyy ==> 456->abc yyy ==> 456->abc zzz ==> 123->abc EOD while ( <$valuesFH> ) { chomp; my( $lev3Key, $upperKeys ) = split m{\s*==>\s*}; my( $lev1Key, $lev2Key) = split m{->}, $upperKeys; $univ{ $lev1Key }->{ $lev2Key }->{ $lev3Key } = 1; } close $valuesFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ %univ ], [ qw{ *univ } ] );

The Data::Dumper output, first of the structure after reading the universe file and second after reading the values file and adding its data.

%univ = ( '456' => { 'abc' => {}, 'xyz' => {} }, '123' => { 'abc' => {} }, '789' => { 'abc' => {} } ); %univ = ( '456' => { 'abc' => { 'xxx' => 1, 'yyy' => 1 }, 'xyz' => {} }, '123' => { 'abc' => { 'zzz' => 1 } }, '789' => { 'abc' => { 'xxx' => 1 } } );

I hope this is helpful.

Cheers,

JohnGG


In reply to Re^3: (another) HoH question by johngg
in thread (another) HoH question by zuma53

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.