1

質問があります。お役に立てれば幸いです。

次の内容を含む 2 つのテキスト ファイルがあります。

FILE1.txt

http://www.dog.com/
http://www.cat.com/
http://www.antelope.com/

FILE2.txt

1
2
Barry

私が正しく達成した出力は次のとおりです。

http://www.dog.com/1
http://www.dog.com/2
http://www.dog.com/Barry 
http://www.cat.com/1
http://www.cat.com/2
http://www.cat.com/Barry
http://www.antelope.com/1 
http://www.antelope.com/2
http://www.antelope.com/Barry

上記を行うコード

    open my $animalUrls, '<', 'FILE1.txt' or die "Can't open: $!";
    open my $directory, '<', 'FILE2.txt' or die "Can't open: $!";

    my @directory = <$directory>;   #each line of the file into an array
    close $directory or die "Can't close: $!";

    while (my $line = <$animalUrls>) {
    chomp $line;
    print $line.$_ foreach (@directory);
    push (@newListOfUrls, $line.$_) foreach (@directory);  #put each new url into array
    }

今私が抱えている問題:

元の URL (File1.txt) の Content Length を取得し、新しい URL のそれぞれの Content-Length を対応する元の URL と比較して、それらが同じか異なるかを確認する必要があります。次に例を示します。

  • http://www.dog.com/1の Content-Length と元の URL http://www.dog.com/の Content-Length を比較して、違いがあるかどうかを確認する必要があります。
  • 次に、http://www.dog.com/2 の Content-Length と元の URL http://www.dog.com/ の Content-Length を比較して違いあるかどうかを確認する必要があります。
  • 次に、http://www.dog.com/Barry の Content-Length と元の URL http://www.dog.com/ の Content-Length を比較して違いあるかどうかを確認する必要があります。
  • 次に、http://www.cat.com/1 の Content-Length と元の URL http://www.cat.com/ の Content-Length を比較して違いあるかどうかを確認する必要があります。
  • 次に、http://www.cat.com/2 の Content-Length と元の URL http://www.cat.com/ の Content-Length を比較して違いあるかどうかを確認する必要があります。
  • 等々........

Content-Length を取得するコード:

print $mech->response->header('Content-Length');  #returns the content length

私が問題を抱えているのは、新しい各 URL を対応する正しい元の URL と比較する方法です。(つまり、 http://www.cat.com/Barry の Content-Length とhttp://www.dog.com/の Content-Length を誤って比較しないください)それ?

これに関するあなたの助けは大歓迎です、どうもありがとう

4

2 に答える 2

3

これにはハッシュを使用する必要があります。入力コードを変更して、より複雑なデータ構造を作成します。これにより、タスクが簡単になります。

open my $animalUrls, '<', 'FILE1.txt' or die "Can't open: $!";
open my $directory, '<', 'FILE2.txt' or die "Can't open: $!";

my @directory = <$directory>;   #each line of the file into an array
close $directory or die "Can't close: $!";
my $newURLs;

while ( my $baseURL = <$animalUrls> ) {
  chomp $baseURL;

  SUBDIR: foreach my $subdir (@directory) {
    chomp $subdir;
    next SUBDIR if $subdir eq "";
    # put each new url into arrayref
    push( @{ $newURLs->{$baseURL} }, $baseURL . $subdir );
  }
}

これを有利に利用できるようになりました。Mechanize が既にセットアップされていると仮定します。

foreach my $url ( keys %{$newURLs} ) {
  # first get the base URL and save its content length
  $mech->get($url);
  my $content_length = $mech->response->header('Content-Length');

  # now iterate all the 'child' URLs
  foreach my $child_url ( @{ $newURLs->{$url} } ) {
    # get the content
    $mech->get($child_url);

    # compare
    if ( $mech->response->header('Content-Length') != $content_length ) {
      print "$child_url: different content length: $content_length vs "
        . $mech->response->header('Content-Length') . "!\n";
    }
  }
}

foreachデータ構造を構築する場所にコードを配置することで、2 番目のループ セットがなくても実行できます。

これらのリファレンスに慣れていない場合は、perlreftutを参照してください。ここで行ったことは、各ベース URL のキーを使用してハッシュを作成し、生成されたすべての子 URL の配列をその中に入れたことです。Data::Dumper を使用して final を出力すると、次の$newURLsようになります。

$VAR1 = {
  'http://www.dog.com/' => [
    'http://www.dog.com/1',
    'http://www.dog.com/2',
   ],
  'http://www.cat.com/' => [
    'http://www.cat.com/1',
    'http://www.cat.com/2',
   ],
};

編集: コードを更新しました。これらのファイルを使用してテストしました。

URL:

http://www.stackoverflow.com/ 
http://www.superuser.com/

方向:

faq
questions
/
于 2013-02-07T11:47:19.747 に答える
1

このコードは、必要なことを行うようです。すべての URL を に格納し、@urls各 URL をフェッチするときにコンテンツの長さを出力します。後で長さのデータが必要な理由はわかりませんが、各応答の長さをハッシュに保存%lengthsして、URL に関連付けました。

use 5.010;
use warnings;

use LWP::UserAgent;

STDOUT->autoflush;

my @urls;

open my $fh, '<', 'FILE1.txt' or die $!;
while (my $base = <$fh>) {
  chomp $base;
  push @urls, $base;
  open my $fh, '<', 'FILE2.txt' or die $!;
  while (my $path = <$fh>) {
    chomp $path;
    push @urls, $base.$path;
  }
}

my $ua = LWP::UserAgent->new;

my %lengths;

for my $url (@urls) {
  my $resp = $ua->get($url);
  my $length = $resp->header('Content-Length');
  $lengths{$url} = $length;

  printf "%s  --  %s\n", $url, $length // 'undef';
}

出力

http://www.dog.com/  --  undef
http://www.dog.com/1  --  56244
http://www.dog.com/2  --  56244
http://www.dog.com/Barry  --  56249
http://www.cat.com/  --  156
http://www.cat.com/1  --  11088
http://www.cat.com/2  --  11088
http://www.cat.com/Barry  --  11088
http://www.antelope.com/  --  undef
http://www.antelope.com/1  --  undef
http://www.antelope.com/2  --  undef
http://www.antelope.com/Barry  --  undef
于 2013-02-07T12:50:03.683 に答える