#!/bin/perl use strict; use warnings; use Data::Dumper; use File::Slurper qw(read_text); sub parse_nested_brackets { my ($string) = @_; my $index = 0; return parse_nested_brackets_recursive($string, \$index); } sub parse_nested_brackets_recursive { my ($string, $index_ref) = @_; my %hash; my ($key, $value); while ($$index_ref < length($string)) { my $char = substr($string, $$index_ref, 1); $$index_ref++; if ($char eq '(') { my $nested_hash = parse_nested_brackets_recursive($string, + $index_ref); if (defined $key) { $hash{$key} = $nested_hash; $key = undef; } else { %hash = (%hash, %$nested_hash); } } elsif ($char eq ')') { if (defined $key && defined $value) { $hash{$key} = $value; $key = undef; $value = undef; } return \%hash; } elsif ($char =~ /\s/) { next; } else { if (!defined $key) { $key = $char; } elsif (!defined $value) { $value = $char; } else { $value .= $char; } } } return \%hash; } my $string = read_text shift; my $hash = parse_nested_brackets($string); print Dumper($hash);
It's not able to parse string to array.

In reply to Re^2: parse lisp style config by vincentaxhe
in thread parse lisp style config by vincentaxhe

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.