in reply to Delete lines if matched expression

DespacitoPerl:

Nice, but I'd work on the code formatting a bit, it's kinda funky, making it difficult to see what you're trying to do. Also, you don't want to use "my" everywhere, only when you're declaring/defining a variable. Further, if you set a default value to your variable, you only need to set it when it would change.

So the bit you have like this:

if (exists $identifier{$pins1}) { if ( ($w1 == $identifier{$pins1}{'w2'}) && ($h1 <= $identifier{$pi +ns1}{'h2'}) ) { my $start = 1; } else { my $start = 0; } } else { my $start = 0; } printf $output "$wline"; next if (my $start == 0);

could easily be changed to:

my $start = 0; if (exists $identifier{$pins1}) { if ( ($w1 == $identifier{$pins1}{'w2'}) && ($h1 <= $identifier +{$pins1}{'h2'}) ) { $start = 1; } } printf $output "$wline"; next if (my $start == 0);

Then, seeing how it's just a chained if statement, you could further reduce it to:

my $start = 0; if ( exists $identifier{$pins1} && ($w1 == $identifier{$pins1}{'w2'}) && ($h1 <= $identifier{$pins1}{'h2'}) ) { $start = 1; } printf $output "$wline"; next if ($start == 0);

There are other improvements you could make.

I might've answered your question, or I might not have, as I didn't actually see a question.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Delete lines if matched expression
by DespacitoPerl (Acolyte) on Jul 26, 2017 at 03:37 UTC

    sry, but what i wish to do is to filter out whatever the pins mentioned inside the waiver file, from the input which is the report file, the condition is if the width matched, and the height in waiver file is greater or equal to the height in input report file, the pins and its following information in report file will be deleted, as a sign it is waived, but the problem now is my script didnt delete the pins and the following information, even they are matched. So anyone of you have any ideas?

      You can't have 2 hash keys as 'es' so you need a 2nd level to your %identifier hash
      eg $identifier{$pins2}{$w2} = $h2

      #! /tools/perl/5.8.8/linux/bin/perl use strict; use warnings; use Data::Dumper; # Source script my $report = $ARGV[1] || 'report.txt' ; my $waiver = $ARGV[3] || 'waiver.csv'; my $result = $ARGV[5] || 'result.txt'; # Set up a hash to receive the information my %identifier = (); # Read the violations file into the hash open my $filter, '<', $waiver or die "Could not open $waiver : $!"; while (my $vline = <$filter>) { next unless $vline =~ /\S/; #skip blank lines $vline =~ s/^\s+//; # trim leading spaces my ($pins2, $w2, $h2) = split /,/, $vline; $identifier{$pins2}{$w2} = $h2; } print Dumper \%identifier; # Read input file line by line and compare 2 files open my $input, '<', $report or die "Could not open $report : $!"; open my $output, '>', $result or die "Could not open $result : $!"; my $header = 1; while (my $wline = <$input>){ # skip checking the heading text if ($header == 1){ print $output $wline; $header = 0 if ($wline =~ /-------/); next; } # delete the contents if matched # undef is leading space my (undef,$pins1, $nets, $w1, $h1, $slack) = split /\s+/, $wline; if (exists $identifier{$pins1}{$w1} && $identifier{$pins1}{$w1} >= $h1) { # skip matched line print "DELETED $wline"; } else { print $output $wline; } } close $filter; close $input; close $output;
      poj