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

我需要帮助:

我需要帮助了解访问散列引用中不同部分的各种方法,并使用它们来做事,例如打印它们。

因此,例如,我如何$url从哈希引用中打印所有内容(即来自 Data::Dumper 的http://www.superuser.com/http://www.stackoverflow.com/

以及如何打印所有$child_url或特定的一个/子集$child_url等等?

非常感谢您对此的帮助,

多谢

4

1 回答 1

1

您可以这样导航您的 hashref:

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

例如,如果您想要超级用户相等分支:

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

更重要的是,要打印 hashref 的 url(第一级键),您可以:

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

然后,如果您想要二级密钥:

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 回答