http://qs1969.pair.com?node_id=473617


in reply to Parsing a macro language

Consider using an existing data serialization language.

I enjoy using YAML for configuration information or other external data. It's advantages are that it is easy to read and write with any text editor, the syntax is fairly intuitive (it looks a lot like outlines done in email), and there are decent parsers and emitters for many common languages, including Perl (YAML).

One of the very nice things about YAML is that it is a direct representation of scalar, array and hash data structures, nested arbitrarily. This means that the language is rich enough to represent any data structure that you can in Perl (or Python, Ruby, JavaScript, PHP, etc.). It also means that you don't have to perform interesting data contortions (or use objects, tree structures, etc.), as you might with XML. Rather, you use the structure directly. It's an AoHoAoH, or what ever you like.

Your data in YAML might look like:
#!/usr/local/bin/perl use YAML; use Data::Dumper; my $data = join '', <DATA>; my $pages = Load( $data ); # YAML does the syntax checking and parsing. # It is still up to you to perform data validation. print Dumper( $pages ); __DATA__ # this is your data, represented in YAML - page: p1 questions: - name: 4B label: Do you like your pie with ice cream? single: - Yes - No - name: 4C label: Do you like your pie with whipped cream? single: - Yes - No # and so on ... # - page: p2 # questions: # -
The Perl data structure looks like this:
$pages = [ { 'page' => 'p1', 'questions' => [ { 'name' => '4B', 'label' => 'Do you like your pie with i +ce cream?', 'single' => [ 'Yes', 'No' ], }, { 'name' => '4C', 'label' => 'Do you like your pie with w +hipped cream?', 'single' => [ 'Yes', 'No' ], } ] } ];

-Colin.

WHITEPAGES.COM | INC