Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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
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 perusing the Monastery: (8)
As of 2014-09-23 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls