Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

How to match lines in one file against lines in another?

I know something is majorly wrong here, but I am new to Perl and looking to do the following:

Find all lines in all.css that contain lines in unused.css and do some logic. The way my code is structured, it seems that I cannot match something like:

if ($lineA =~ /$lineU/) #if line in all.css contains line in unused.css

since the variables are being defined separately.

How would I structure the program to be able to match lines in all.css against lines in unused.css?

My program is below:

#!/usr/bin/perl

use strict;
use warnings;

open(my $unused_handle,'<', "unused.css") or die $!;
open(my $all_handle,'<',"all.css") or die $!;
open(my $out, '>' ,'lean.css') or die $!;

my $lineU = q{};
my $lineA = q{};

print $out "$lineU";

while($lineU =<$unused_handle>) {

    print $out "$lineU";
    #print $out "$lineA";  Line One not printed from All
    while($lineA =<$all_handle>) {

        if ($lineA =~ m/$lineU/sxm) {

            print "Huza!\n";
        }

        else {
            print "No Match\n";
        }

    }

}

close ($unused_handle);
close ($all_handle);
close ($out);

print "Done!\n";

exit;

An example of my input files is below.

Example lines from unused.css:

audio, canvas, video
audio:not([controls])
[hidden]
h6

Example lines from all.css:

article, aside, details, figcaption, figure, footer, header, hgroup, nav, section, summary {
    display: block;
}
audio, canvas, video {
    display: inline-block;
    *display: inline;
    *zoom: 1;
}
audio:not([controls]) {
    display: none;
    height: 0;
}
[hidden] {
    display: none;
}

Upvotes: 1

Views: 651

Answers (2)

Jost
Jost

Reputation: 1569

I hope this (untested) snippet helps you a bit:

#!/usr/bin/perl

use strict;
use warnings;

open(my $unused,'<', "unused.css") or die $!;
open(my $all,'<',"all.css") or die $!;

# store all lines of unused.css in a hash
my %unused_line;
while (<$unused>) {
    #remove newlines
    chomp();
    #store the line as a key with empty value
    %unused_line{$_}="";
}
close ($unused);

#...for every line in all.css
while (<$all>) {
    #have we seen it in unused.css (at least everything before ' {')
    if ((m/^(.*\S+)\{/) && (exists $unused_line{$1}))
    {
        #a match - found a line of unused.css in all.css
    }else{
        #no match  - line does not exists in unused.css
    }
}
close ($all);

Upvotes: 1

manchicken
manchicken

Reputation: 155

Try:

if ($lineA =~ m/$lineU/sxm)

Also, consider the possibility that you may have differing line endings in the file, and strip line endings prior to performing your comparison.

Finally, I'm hoping that you recognize that you're ignoring the first line of each file by pulling a line prior to starting your while loops.

my $lineU = <$unused>;
my $lineA = <$all>;

If you didn't want to do this, it would be better to initialize as such:

my $lineU = q{};
my $lineA = q{};

Upvotes: 1

Related Questions