Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Aid for Parse::RecDescent-er(s)

by hsmyers (Canon)
on Jun 24, 2002 at 01:42 UTC ( [id://176665]=CUFP: print w/replies, xml ) Need Help??

I've recently been involved with a 'small language' project using (of course) Parse::RecDescent and while thrashing about developed a tool that has made my life a good deal simpler. Perhaps by extension it could be useful to others as well.

#!/usr/bin/perl # recurse.pl -- Aid to Parse::RecDescent builders use warnings; use diagnostics; use Data::Dumper; my @data = ( 'level0', ['level1','(',['level2',['level3','line1','line2','line3 +']]] ); print Dumper @data,"\n"; recurse (\@data); sub recurse { my $tree = shift; my $indent = shift; my $level_count = shift; my $count = 0; unless ( defined($indent) ) { $indent = 0; } unless ( defined($level_count) ) { $level_count = ""; } foreach (@$tree) { if ( ref eq 'ARRAY' ) { printf( "%40s |%s[\n", sprintf( "%s%s", $level_count, $cou +nt ), '.' x $indent ); recurse( $_, $indent + 2, $level_count . "$count." ); $count++; printf( "%40s |%s]\n", " ", '.' x $indent ); } else { if ( !defined($_) ) { printf( "%40s |%s%s\n", sprintf( "%s%s", $level_count, $count++ ), '.' x $in +dent, "!Undefined!" ); } elsif ( $_ eq "" ) { printf( "%40s |%s%s\n", sprintf( "%s%s", $level_count, $count++ ), '.' x $in +dent, "!NULL!" ); } elsif ( $_ eq "\n" ) { printf( "%40s |%s%s\n", sprintf( "%s%s", $level_count, $count++ ), '.' x $in +dent, '\\n' ); } else { printf( "%40s |%s%s\n", sprintf( "%s%s", $level_count, $count++ ), '.' x $in +dent, $_ ); } } } }

Which if you run produces:

$>recurse $VAR1 = 'level0'; $VAR2 = [ 'level1', '(', [ 'level2', [ 'level3', 'line1', 'line2', 'line3' ] ] ]; $VAR3 = ' '; 0 |level0 1 |[ 1.0 |..level1 1.1 |..( 1.2 |..[ 1.2.0 |....level2 1.2.1 |....[ 1.2.1.0 |......level3 1.2.1.1 |......line1 1.2.1.2 |......line2 1.2.1.3 |......line3 |....] |..] |]

The first portion is obviously the output of the call to Dumper and should present no particular mystery to any one familiar with that excellent module. The second portion however strangely formatted it may appear is my own concoction. You will note that it is centered within an 80 column format. Why?--because it grows from either side depending on depth of the data structure. Next, the material on the right hand side obviously is the same as what you would receive from Dumper--although formatted in a fashion that makes alignment of array indicators a bit more legible. This leaves the odd arrangement of numbers on the left hand side. These are in fact, the indices of the array displayed. For instance consider the following list:

  • 0, as the index of @$tree[0] has the value of 'level0'
  • 1.0, as the indices of @$tree[1]->[0] has the value of 'level1'
  • 1.2.0, as the indices of @$tree[1]->[2]->[0] has the value of 'level2'
and so on...

The chief value of this function comes when you begin to process portions of the tree and need to easily see just what it was that got passed to you! It doesn't take much work with parse trees to notice that they quickly grow to a very large size--often too large to easily keep track of. And even if printed, usually too large to deal with that way as well. After doing more work with 'print' this that and the other thing than perhaps I should admit to, I finally came apon this approach! I suppose that you might think of it as having the virture of the Dumper approach as a map, with the added benefit clearly marked labels. Come to think of it, there is no particular reason that this couldn't be usful where ever you need to know just how to get your hands on a particular 'thing' buried within a typical perl data structure!

Happy data mining...

–hsm

"Never try to teach a pig to sing…it wastes your time and it annoys the pig."

Edited: ~Wed Jul 3 17:39:28 2002 (GMT), by footpad:
Corrected spelling of module in title, per Consideration

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://176665]
Approved by jepri
Front-paged by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found