Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Some time back I had to do something similar. While I am not able to pull example code at the moment, what I did was basically the following:

    1. Read each line from the config into a file.
    2. Keep a count of the number of leading spaces on each line in a hash.
  1. Compute the greatest common factor (gcf) of the non-zero leading space counts.

I then processed the array of entries using the ( number of leading spaces / GCF ) value to determine indentation level.

For the example configuration you show, the indentation would show as the following:

%indentation = ( '0' => 1, '2' => 3, '4' => 2, );

Because I was potentially looking at multiple indentation levels, I believe I did something like the following to get the common indentation width:

# Yes, sometimes I write code in ways to make sure _I_ # remember what I was doing when I come back to it later. sub gcf { my ( $i, $j ) = sort { $b <=> $a} @_; return( abs( $j ) ) if ( $i == 0 ); return( abs( $i ) ) if ( $j == 0 ); return( abs( gcf( $j, $i % $j ) ); } my $common_indent_factor = 1; my @data = sort { $b <=> $a } keys %indentation; while ( scalar @data ) { my $f_1 = shift @data; my $f_2 = shift @data; my $f_temp = $common_indentation_factor = gcf( $f_1, $f_2 ); push @data, $f_temp; }

This resulted in a common indentation factor of 2, which agrees with the observed.

Hope that helps.


In reply to Re: Parsing by indentation by atcroft
in thread Parsing by indentation by llarochelle

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 about the Monastery: (6)
As of 2024-04-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found