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

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

by hdb (Prior)
on Apr 29, 2013 at 12:12 UTC ( #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";


Comment on Re: Change the data to have the format which is like a C struct.
Download Code
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
Node Status?
node history
Node Type: note [id://1031201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2015-07-07 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls