Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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


In reply to Aid for Parse::RecDescent-er(s) by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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-28 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found