Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Change the data to have the format which is like a C struct.

by hdb (Monsignor)
on Apr 29, 2013 at 12:12 UTC ( [id://1031201]=note: print w/replies, xml ) Need Help??


in reply to Change the data to have the format which is like a C struct.

Now it works for more complex structures as well as long as they are wellformed.

use strict; use warnings; my $string = <<STRUCT; a = 0 mm struct a = 1 b = 3 m1 struct = c = 4 d = 5 m2 struct = f = 9 e = 7 m3 struct = h = 124 STRUCT my @lines = split /\n/, $string; my @levels; my $ind = 0; my $oldind; for( my $i=0; $i<@lines; ++$i ) { $oldind = $ind; $lines[$i] =~ /^(\s*)/; $ind = length( $1 ); push @levels, $ind if $i==0; $lines[$i] .= ";" if $lines[$i] =~ /\S+\s*=\s*\S+/; next if $i==0 or $ind == $oldind; if( $ind > $oldind ) { $lines[$i-1] .= " {"; push @levels, $ind; } elsif( $ind < $oldind ) { while( $levels[-1] > $ind ) { pop @levels; $lines[$i-1] .= "\n"." "x$levels[-1]."}"; } } } while( $levels[-1] > $levels[0] ) { pop @levels; $lines[-1] .= "\n"." "x$levels[-1]."}"; } print join "\n", @lines, "\n";

Replies are listed 'Best First'.
Re^2: Change the data to have the format which is like a C struct.
by dwslovedh (Novice) on May 04, 2013 at 03:37 UTC

    It works, Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1031201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-16 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found