Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Need to save a single line from delete on some special condition

by clueless newbie (Deacon)
on Mar 26, 2019 at 16:41 UTC ( #1231709=note: print w/replies, xml ) Need Help??


in reply to Need to save a single line from delete on some special condition

Maybe you want to look at Regexp::Common::balanced. Using it I get this: Updated and cleaned up in response to anirbanphy's post which follows:
use Data::Dumper; use Regexp::Common; use strict; use warnings; my $pin_group_re=qr{ pin\ + # "pin" followed by sp +aces ( # $1 capture $RE{balanced}{-parens=>'()'} # balanced parenthesis \ + # followed by spaces $RE{balanced}{-parens=>'{}'} # balanced braces ) # end $1 capture }x; my $timing_group_re=qr{ timing\ + # "timing" followed by + spaces ( # $1 capture $RE{balanced}{-parens=>'()'} # balanced parenthesis \ + # followed by spaces $RE{balanced}{-parens=>'{}'} # balanced braces ) # end $1 capture }x; sub do_something_with_a_pin { # To see what we got we'll dump @_ warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my $string=shift; if ($string =~ m{direction : input}) { # Has a "direction : in +put" so no modification warn "has 'direction : input'"; } elsif ($string =~ m{$timing_group_re}) { # has a timing group warn "has 'timing'"; # kill the max_transition line -- $string =~ s{^.*max_transition.*\r?\n}{}m; # Let's see what the string looks like now }; warn Data::Dumper->Dump([\$string],[qw(*string)]),' '; return $string; }; 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 pin groups ($string=<DATA>) =~ s{$pin_group_re}{"pin\ ".do_something_with_a_p +in($1)}gex; } print $string; __DATA__ IN_FILE cell (lib_1) { dont_use : true ; dont_touch : true ; pin ("HIZIBI_IN_1") { direction : input ; clock : true ; max_transition : 1 ; capacitance : 12 ; } pin ("HIZIBI_79") { direction : output ; max_transition : 10; min_capacitance : 3 ; } pin ("HIZIBI_IN_1") { direction : input ; clock : true ; max_transition : 1 ; capacitance : 1 ; } pin ("HIZIBI_78") { direction : output ; max_transition : 10; min_capacitance : 34 ; capacitance : 34 ; } pin ("HIZIBI") { direction : output ; clock : true ; max_transition : 20; related_power_pin : VDD ; related_ground_pin : VSS ; timing () { cell_fall (into_f1) { index_1("1,2,3,4,5") ; index_2("1,2,3,4,5") ; values("13, 13, 14, 16, 18",\ "13, 14, 15, 16, 19",\ "14, 15, 16, 17, 20",\ "15, 15, 16, 18, 20",\ "15, 16, 17, 18, 21") ; } } } }
Now the last pin group looks like
pin ("HIZIBI") { direction : output ; clock : true ; related_power_pin : VDD ; related_ground_pin : VSS ; timing () { cell_fall (into_f1) { index_1("1,2,3,4,5") ; index_2("1,2,3,4,5") ; values("13, 13, 14, 16, 18",\ "13, 14, 15, 16, 19",\ "14, 15, 16, 17, 20",\ "15, 15, 16, 18, 20",\ "15, 16, 17, 18, 21") ; } }

Replies are listed 'Best First'.
Re^2: Need to save a single line from delete on some special condition
by anirbanphys (Beadle) on Mar 28, 2019 at 09:27 UTC

    Hello clueless newbie, I was using your code and trying to include one more if condition.

    As you already included the logic if "timing" (balanced parenthesized delimiter) present then only by searching the pattern "max_transition", it will delete the line.

    Now I need to add one more condition, you might observed that if "direction : input" (for any pin group) condition arise, it will never delete "max_transition" line. And according to the given code, it will delete "max_transition" line for a input pin group where "timing" is present, which I do not want.

    So I tried to include this

    sub do_something_with { #warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my $string=shift; if ( $RE{some}{pattern}->matches(~m/^\s*direction\s*:\s*output +\s*;/) ) { if ($string =~ m{timing +($RE{balanced}{-parens=>'()'} +$RE{ba +lanced}{-parens=>'{}'})}) { # has a timing group # kill the max_transition line $string =~ s{^.*max_transition.*\r?\n}{}m; #warn Data::Dumper->Dump([\$string],[qw(*string)]),' '; }; #return $string; }; return $string; };

    But while I am trying to execute I am getting error.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2019-12-12 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?