Okay. Have a better appreciation of your constraints. Have played around with your code a bit and I think it covers all of the cases that you showed well. There may be a bit of an opportunity to make the config file syntax a little less visually cluttered though.
If you can trust that there are empty lines between the blocks and not within, you can strip off the need for the > or < symbols at the start of each line. Also if you just assume an empty selector means >, then you only have to place the < selectors in cases where you don't want to nudge the index forward. Check out the example DATA below the code. The code will still parse your original example as well if you don't like these changes.
All bugs are mine after twisting your code around like this:
use strict; use warnings; use Data::Diver qw( DiveVal DiveError ); use Data::Dumper qw( Dumper ); my $state = { }; my $ref = [ ]; my $prefix = '<'; while (<DATA>) { chomp; next if /^\s*#.*$/; if (/^\s*$/) { $prefix = '>'; next; }; my ($selector, $value) = split /\s*=\s*/; next unless defined $selector; my @selector = split /\./, $selector =~ s/\.$/.>/r; unshift(@selector, $prefix) unless $selector =~ /^[><]\./; _dive( $ref, \@selector, $value ); $prefix = '<'; } print Dumper $ref; sub _dive { my ( $ref, $selector, $value ) = @_; return unless defined $ref and defined $selector and scalar @$sele +ctor; my @selector_b; for ( @$selector ) { if ( /^(>|<|)$/ ) { my $selector_b = join '.', @selector_b; if ( $1 eq '<') { push @selector_b, $state->{$selector_b} //= 0; } else { $state->{$selector_b} += defined $state->{$selector_b} + ? 1 : 0; push @selector_b, $state->{$selector_b}; } } else { push @selector_b, $_; } } DiveVal( $ref, @selector_b ) = $value; my ( $error ) = DiveError( ); $error and warn $error; return 1; } __DATA__ name = john location = uk interests. = programming interests. = cycling name = laura location = interests. = knitting interests. = tennis interests. = dancing name location = canada interests.. = dogs interests.<. = horses interests. = cars # test.error = blah
In reply to Re^3: Dive data with automatic array indexing
by Loops
in thread Dive data with automatic array indexing
by peterp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |