-1

私はあなたが助けてくれることを望んでいる質問がありますか?

これは、ハッシュ参照を理解する上で助けが必要な最後の部分です

コード:

my $content_lengths; # this is at the top
foreach my $url ( # ... more stuff

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

  # store the urls that are found to have different content
  # lengths to the base url only if the same url has not already been stored
  $content_lengths->{$url}->{'different'}->{$child_url} = $mech->response->header('Content-Length');

} elsif ( $mech->response->header('Content-Length') == $content_length ) {
  print "Content lengths are the same\n";

  # store the urls that are found to have the same content length as the base
  # url only if the same url has not already been stored
  $content_lengths->{$url}->{'equal'}->{$child_url} = $mech->response->header('Content-Length');
}

Data::Dumperを使用したときの様子

$VAR1 = {
      'http://www.superuser.com/' => {
                                       'difference' => {
                                                         'http://www.superuser.com/questions' => '10735',
                                                         'http://www.superuser.com/faq' => '13095'
                                                       },
                                       'equal' => {
                                                    'http://www.superuser.com/ ' => '20892'
                                                  }
                                     },
      'http://www.stackoverflow.com/' => {
                                           'difference' => {
                                                             'http://www.stackoverflow.com/faq' => '13015',
                                                             'http://www.stackoverflow.com/questions' => '10506'
                                                           },
                                           'equal' => {
                                                        'http://www.stackoverflow.com/ ' => '33362'
                                                      }
                                         }
    };

助けが必要なもの:

ハッシュ参照のさまざまな部分にアクセスし、それらを使用して印刷などを行うさまざまな方法を理解するための支援が必要です。

たとえば、ハッシュ参照からすべてを印刷するにはどうすればよいですか(つまり、 http ://www.superuser.com/およびhttp://www.stackoverflow.com/$urlになるData :: Dumperから)

また、すべて$child_urlまたは特定の1つ/サブセットを印刷するにはどうすればよい$child_urlですか?

これに関するあなたの助けは大いに感謝されます、

どうもありがとう

4

1 に答える 1

1

したがって、hashrefをナビゲートできます。

$hashref->{key1}{key2}{keyN};

たとえば、スーパーユーザーのequalブランチが必要な場合:

my $urlArrayref = $hashref->{'http://www.superuser.com/'}{'equal'};

さらに重要なことに、hashrefのURL(第1レベルのキー)を出力するには、次のようにします。

foreach my $key ( keys( %{$hashref} ) ) {
    print( "key is '$key'\n" );
}

次に、第2レベルのキーが必要な場合:

foreach my $firstLevelKey ( keys( %{$hashref} ) ) {
    print( "first level key is '$firstLevelKey'\n" );
    foreach my $secondLevelKey ( keys( %{$hashref->{$firstLevelKey}} ) ) {
        print( "\tfirst level key is '$secondLevelKey'\n" );
    }
}

など...

- - - 編集 - - -

これは、上記の例のサンプルコードです。

#!/usr/bin/perl

use strict;
use warnings;

my $content_lengths = {
    'http://www.superuser.com/' => {
        'difference' => {
            'http://www.superuser.com/questions' => '10735',
            'http://www.superuser.com/faq' => '13095'
        },
        'equal' => {
            'http://www.superuser.com/ ' => '20892'
        }
    },
    'http://www.stackoverflow.com/' => {
        'difference' => {
            'http://www.stackoverflow.com/faq' => '13015',
            'http://www.stackoverflow.com/questions' => '10506'
        },
        'equal' => {
            'http://www.stackoverflow.com/ ' => '33362'
        }
    }
};

foreach my $key1 ( keys( %{$content_lengths} ) ) {
    print( "$key1\n" );
    foreach my $key2 ( keys( %{$content_lengths->{$key1}} ) ) {
        print( "\t$key2\n" );
        foreach my $key3 ( keys( %{$content_lengths->{$key1}{$key2}} ) ) {
            print( "\t\t$key3\n" );
        }
    }
}

これにより、次の出力が得られます。

http://www.superuser.com/
        difference
                http://www.superuser.com/questions
                http://www.superuser.com/faq
        equal
                http://www.superuser.com/
http://www.stackoverflow.com/
        difference
                http://www.stackoverflow.com/faq
                http://www.stackoverflow.com/questions
        equal
                http://www.stackoverflow.com/
于 2013-02-13T17:25:12.163 に答える