Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to add a new line after three or four lines from a pattern after getting that pattern

by clueless newbie (Deacon)
on Apr 03, 2019 at 15:03 UTC ( #1232099=note: print w/replies, xml ) Need Help??


in reply to How to add a new line after three or four lines from a pattern after getting that pattern

I'm somewhat lazy so
#!/usr/bin/env perl use Data::Dumper; use Regexp::Common; use 5.12.0; my $library_group_re=qr{ library\ * # "library" follow +ed by optional spaces ( # $1 capture (?<paren>$RE{balanced}{-parens=>'()'}) # balanced parenth +esis \ * # followed by opti +onal spaces (?<braces>$RE{balanced}{-parens=>'{}'}) # balanced braces ) # end $1 capture }x; my $operating_conditions_group_re=qr{ operating_conditions\ * # "operating_condi +tions" followed by optional spaces ( # $1 capture (?<paren>$RE{balanced}{-parens=>'()'}) # balanced parenth +esis \ * # followed by opti +onal spaces (?<braces>$RE{balanced}{-parens=>'{}'}) # balanced braces ) # end $1 capture }x; sub do_something_with_an_operating_conditions { #warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my (%captured)=@_; if (!exists $captured{paren} || ! exists $captured{braces}) { die "Should NOT happen!"; }; my $return='operating_conditions '; # process the stuff within the parenthesis - $return.="$captured{paren} "; # process the stuff within the braces $return.="$captured{braces}"; # The additional line. $return.="\n default_operating_conditions : ".substr($captured +{paren},1,-1)."\n"; return $return; }; sub do_something_with_a_library { #warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my (%captured)=@_; if (!exists $captured{paren} || ! exists $captured{braces}) { die "Should NOT happen!"; }; my $return='library '; # process the stuff within the parenthesis - $return.="$captured{paren}"; # process the stuff within the braces - operating conditions { (my $string=$captured{braces})=~ s{$operating_conditions_gr +oup_re}{do_something_with_an_operating_conditions(%+)}gex; $return.=" $string"; }; return $return; }; my $string; { # Because we want all of the data as a single string local $/; # Deal with string, which will be the entire file, replacing each +of library groups ($string=<DATA>) =~ s{$library_group_re}{do_something_with_a_libra +ry(%+)}gex; } print $string; __END__ library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }
  • Comment on Re: How to add a new line after three or four lines from a pattern after getting that pattern
  • Download Code

Replies are listed 'Best First'.
Re^2: How to add a new line after three or four lines from a pattern after getting that pattern
by anirbanphys (Beadle) on Apr 03, 2019 at 19:41 UTC
    Hello clueless newbie,

    It is great to see you have resolved my problem in another way. I specially like the subroutine you have created, believe me I was going to post one more code issue which I was trying to resolve, but after looking and understanding your code I successfully accomplished the job :). What a moment of joy.

    Thank you. Thanks a TON

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2019-11-21 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (103 votes). Check out past polls.

    Notices?