in reply to Dive data with automatic array indexing
Hi Chris, I'm not sure exactly your end goal, or whether the $VAR1 data from the Dumper output you show is valuable to you at all. But if not you might consider using one of the config file parsers from CPAN. It might take some coercing to get one to conform to your needs, but you seem to have the flexibility to change the format of your files; an automated translation should be doable. Plus you'll get the escaping and quoting features, that you mention are lacking in your code.
Here's a quick example
use strict; use warnings; use Data::Dumper; use Config::Scoped; my $cs = Config::Scoped->new(); my @lines = ("data{all = [\n", <DATA>, "]}\n"); my $Config = $cs->parse(text => join('', @lines))->{data}->{all}; print Dumper($Config); __DATA__ { name = john location = uk interests = [ programming cycling ] } # An ignored comment { name = laura interests = [ knitting tennis dancing ] } { location = canada interests = [[ dogs horses ] cars] }
Which displays:
$VAR1 = [ { 'interests' => [ 'programming', 'cycling' ], 'location' => 'uk', 'name' => 'john' }, { 'interests' => [ 'knitting', 'tennis', 'dancing' ], 'name' => 'laura' }, { 'location' => 'canada', 'interests' => [ [ 'dogs', 'horses' ], 'cars' ] } ];
If you absolutely need the keys that have undefined values as in your example output, the code can be changed to include them. It should be as easy to write a translator from your existing files into this format, as for the new syntax you are proposing. However, other CPAN options might be more to your liking.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Dive data with automatic array indexing
by peterp (Sexton) on Oct 27, 2014 at 06:25 UTC | |
by Loops (Curate) on Oct 27, 2014 at 09:43 UTC | |
by peterp (Sexton) on Oct 27, 2014 at 13:51 UTC |