in reply to Populating hash keys and LDAP: putting things together

What I need is to create the following hash:
$href->{p=root}{cp=1} = 'entry1' $href->{p=root}{cp=1}{n=1} = 'entry11'
Uh-uh. You can't have both. You see, if your create the latter first, then $href->{'p=root'{'cp=1'} will be a hash ref. And if you'd try to create them in the order above, you'll end up using 'entry1' as a symbolic reference for a hash %entry1, thus setting $entry1{'n=1'} = 'entry11';.

With the data you've given, the only thing you can populate, is the following structure — even though I don't understand the structure of your values:

$href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=1'} = 'entry1111'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=2'} = 'entry1112'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=3'} = 'entry1113'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=1'} = 'entry1121'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=2'} = 'entry1122'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=3'} = 'entry1123'; # Let's see what we got: use Data::Dumper; $Data::Dumper::Indent = 1; print Dumper $href;
Resulting in:
$VAR1 = { 'p=root' => { 'cp=1' => { 'n=1' => { 'n=1' => { 'n=1' => { 'n=1' => 'entry1111', 'n=2' => 'entry1112', 'n=3' => 'entry1113' }, 'n=2' => { 'n=1' => 'entry1121', 'n=2' => 'entry1122', 'n=3' => 'entry1123' } } } } } };
That's it. You can't put anything more in it. Not like his. You might have to find another data structure type to hold your data. Or you might have to reduce the amount of data you want to hold.

Replies are listed 'Best First'.
Re: Re: Populating hash keys and LDAP: putting things together
by dda (Friar) on Oct 07, 2003 at 11:17 UTC
    Thanks. Now it is much more clear than before. I need to store a kind hierarchical menu, and values should be assigned to all nodes, not only to leaf nodes. That way, I need not only
    $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=1'} = 'entry1111';
    but also
    $href->{'p=root'}{'cp=1'} = 'entry1'; $href->{'p=root'}{'cp=1'}{'n=1'} = 'entry11'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'} ='entry111';
    What data structore can be used for this?

    --dda

      Well... one thing you could do, is when your data has a specific format — and it looks like it does, judging by your example, that everything contains an "=" sign — is add a special hash key for your normal values. I'd propose a '$', reminescent of the string suffix in BASIC, or the scalar sigil in Perl if you like.
      $href->{'p=root'}{'cp=1'}{'$'} = 'entry1'; $href->{'p=root'}{'cp=1'}{'n=1'}{'$'} = 'entry11'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'$'} ='entry111'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'$'} ='entry1111';
      That should work well, without a conflict.

      If you need more kinds of (meta-)data, you can add more special keys.