1

ここで何かが大きく間違っていることは知っていますが、私は Perl に不慣れで、次のことをしようとしています:

unused.css 内の行を含む all.css 内のすべての行を検索し、いくつかのロジックを実行します。私のコードが構造化されている方法は、次のようなものと一致しないようです:

if ($lineA =~ /$lineU/) #all.css の行に unused.css の行が含まれている場合

変数が別々に定義されているためです。

all.css の行と unused.css の行を一致させるには、プログラムをどのように構成すればよいでしょうか?

私のプログラムは以下の通りです:

#!/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;

私の入力ファイルの例を以下に示します。

unused.css の行の例:

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

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;
}
4

2 に答える 2

1

試す:

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

また、比較を実行する前に、ファイル内の行末が異なる可能性があることを考慮して、行末を取り除いてください。

最後に、while ループを開始する前に行をプルすることで、各ファイルの最初の行を無視していることに気付いてほしいと思います。

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

これを行いたくない場合は、次のように初期化することをお勧めします。

my $lineU = q{};
my $lineA = q{};
于 2013-08-26T16:11:59.803 に答える
1

この(テストされていない)スニペットが少し役立つことを願っています:

#!/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);
于 2013-08-26T16:21:20.150 に答える