【问题标题】:Perl WWW:Mechanize Accessing data in a hash referencePerl WWW:Mechanize 访问散列引用中的数据
【发布时间】:2013-01-29 08:00:40
【问题描述】:

我有一个问题希望您能提供帮助?

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

代码:

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

非常感谢您的帮助,

非常感谢

【问题讨论】:

    标签: perl hash www-mechanize hashref


    【解决方案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/
    

    【讨论】:

    • 嗨,你能告诉我我的例子的代码是什么样的吗,就像当我使用你的例子作为模板做一个foreach时,我得到了诸如“类型”之类的错误arg 1 到键必须是散列(不是数组取消引用)'?感谢您的帮助
    • 如果我使用foreach my $here (keys %{$content_lengths} ) { print( "key is '$here'\n"); },我无法获取一些数据?但当我使用 foreach my $key ( keys( @{$content_lengths} ) ) { print( "key is '$key'\n" ); } 时不会?
    • @perl-user,对不起,这是一个错字...应该是keys( %{$content_lengths} ),正如你所想的那样。我在上面更新了我的答案。
    • @perl-user,这能回答你的问题吗?
    • @perl-user,你肯定有错字什么的。我测试了这段代码,见上面的编辑。它有效。
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2013-05-09
    • 2016-09-08
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多