in reply to Parsing a dataset into an arbitrary sized hash of hashes

The problem seems to be the absence of types in Perl: if DiveVal encounters a number, it creates an array for you, not a hash:
$ perl -MData::Dumper -MData::Diver=DiveVal -E 'DiveVal($x = {}, qw(1 +2 3)); say Dumper $x' $VAR1 = { '1' => [ undef, undef, [ undef, undef, undef, undef ] ] };

BTW, I had to rewrite your code to be able to understand what it does:

#!/usr/bin/perl use warnings; use strict; use File::Find; use Data::Dumper; use Data::Diver qw (DiveVal); my @extracted = <DATA>; push @extracted, 'end'; my @currentpath = (); my $structure = {}; for my $i (0 .. $#extracted) { my @list = split /;/, $extracted[$i]; my @list2 = split /;/, $extracted[$i+1]; if ($list2[0] eq 'end') { last } if ($list2[0] gt $list[0]) { push @currentpath, $list[1]; } else { my @req = (@currentpath, $list[1], 'required'); DiveVal($structure, @req) = $list[3]; pop @currentpath if $list2[0] lt $list[0]; } } print Dumper($structure);

Update: See Re^3: Parsing a dataset into an arbitrary sized hash of hashes on how to fix the problem (line 28 of my code).

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Parsing a dataset into an arbitrary sized hash of hashes
by Anonymous Monk on Apr 02, 2014 at 23:35 UTC

    The problem seems to be the absence of types in Perl: if DiveVal encounters a number, it creates an array for you, not a hash:

    :) The documentation disagrees -- you can specify \2 if you want to force hash keys

    Note that all 'keys' that work for arrays also work for hashes. If you have a reference that is overloaded such that it can both act as an array reference and as a hash reference or, in the case of DiveVal() and DiveRef(), if you have an undefined $ref which can be autovivified into either type of reference, then numeric-looking key values cause an array dereference. In the above cases, if you want to do a hash dereference, then you need to pass in a reference to the key.
      Great! The following change then solves the problem:
      my @req = map \$_, @currentpath, $list[1], 'required';
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        This worked perfectly! Thank you very much!

        Shawn Way