Perl WWW:Mechanize访问哈希引用中的数据

时间:2013-02-13 17:21:02

标签: perl hash www-mechanize hashref

我有一个问题,我希望你能提供帮助吗?

这是我在理解哈希引用时需要帮助的最后一部分

代码:

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特定的一个/子集等等?

非常感谢您对此的帮助,

非常感谢

1 个答案:

答案 0 :(得分: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/