Hi All (again),
  Sorry for all the messages today. I've got a deadline that has made me put in some stupid hours and my brain has stopped functioning properly :s
I've got a variable update form that is generated based on a hash reference called $Config. The submitted data comes in similar to what is shown below:-
my %inputdata = ( 'Config->{blah}->{hat}' => "splat", 'Config->{cat}->[0]' => "doh", 'Config->{cat}->[1]' => "ray", );
Now if I want to load this data it's easy with eval:-
my $evaltext; foreach my $key (keys %inputdata) { $evaltext .= "\$$key = qq~$inputdata{$key}~;\n"; }#foreach eval $evaltext;

But I don't like eval's overhead, so I usually try to avoid it. The problem is in doing so I've ended up with some really stupid code.. that works, but surely isn't the best way to do it:-
foreach my $key (keys %inputdata) { my @keys = split(/->/, $key); # if ($#keys == 0) { # ${$keys[0]} = $inputdata{$key}; # }#if if ($#keys == 1) { if ($keys[1] =~ /\{/) { $keys[1] =~ s/(\{|\})//g; ${$keys[0]}->{$keys[1]} = $inputdata{$key}; }#if if ($keys[1] =~ /\[/) { $keys[1] =~ s/(\[|\])//g; ${$keys[0]}->[$keys[1]] = $inputdata{$key}; }#if }#if if ($#keys == 2) { if ($keys[1] =~ /\{/) { $keys[1] =~ s/(\{|\})//g; if ($keys[2] =~ /\{/) { $keys[2] =~ s/(\{|\})//g; ${$keys[0]}->{$keys[1]}->{$keys[2]} = $inputdata{$key} +; }#if if ($keys[2] =~ /\[/) { $keys[2] =~ s/(\[|\])//g; ${$keys[0]}->{$keys[1]}->[$keys[2]] = $inputdata{$key} +; }#if }#if if ($keys[1] =~ /\[/) { $keys[1] =~ s/(\[|\])//g; if ($keys[2] =~ /\{/) { $keys[2] =~ s/(\{|\})//g; ${$keys[0]}->[$keys[1]]->{$keys[2]} = $inputdata{$key} +; }#if if ($keys[2] =~ /\[/) { $keys[2] =~ s/(\[|\])//g; ${$keys[0]}->[$keys[1]]->[$keys[2]] = $inputdata{$key} +; }#if }#if }#if }#foreach
It would also quickly break and need to be updated if the data structure became deeper. Feels like brain it's up my a** at the moment, could someone give me a clout and point me onto the right way to do it?

Thanks

Lyle

In reply to Creating data tree from input without eval by cosmicperl

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.