在Perl中使用数据引用的正确方法 [英] The correct way to play with data references in Perl

查看:121
本文介绍了在Perl中使用数据引用的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组要处理的数据.

I have a set of data that I would like to process.

为了简化我的代码,最好通过指向原始数据的引用数组访问我的数据的某些子集.

In order to simplify my code, It would be nice to access to some subsets of my data through array of references that point to the original data.

除了解释,我还写下了这个示例(目前尚无法正常工作).最后,我想更新原始数据,而不必同时更新所有子集.

Better than an explanation, I wrote down this example (which is not working yet). At the end, I would like to update the original data without having to also update all the subsets.

是否可以用Perl做类似的事情?

Is it possible to do something like this with Perl ?

#!/usr/bin/env perl
use strict;
use warnings;

# A set of data
my $design = {
    box => {
        ne =>  {data => 'north-east'},
        nw =>  {data => 'north-west'},
        n  =>  {data => 'north'},
        s  =>  {data => 'south'},
        e  =>  {data => 'east'},
        w  =>  {data => 'west'},
        se  => {data => 'south-east'},
        sw  => {data => 'south-west'}
    }
};

# Select a design
my $selected = 'box';

# Build some arrays
my $d = $design->{$selected};

my @eastside  = (\$d->{e}, \$d->{ne}, \$d->{se});
my @westside  = (\$d->{w}, \$d->{nw}, \$d->{sw});
my @northside = (\$d->{n}, \$d->{ne}, \$d->{nw});

# Update one data
$d->{ne}->{data} .= " updated!";

# Display
print join '', "Composed of:\n", map("\t".$_->{data}."\n", @eastside);

脚本应输出:

Composed of:
   east
   north-east updated!
   south-east

推荐答案

所有错误是您正在引用已经是哈希引用的值的引用,如

All that is wrong is that you are taking a reference of values that are already hash references in the lines like

my @eastside  = (\$d->{e}, \$d->{ne}, \$d->{se})

您应该简单地省略反斜杠,然后一切正常.

You should simple omit the backslashes and everything will work.

(顺便说一句,您可能想知道引用\运算符是分布式的,因此您可以使用

(By the way, you may want to know that the reference \ operator is distributive, so you can write the same thing with

my @eastside  = \( $d->{e}, $d->{ne}, $d->{se} )

但是那样就不再正确了!)

but it's no more correct that way!)

其他几点

  • 在这种情况下(您需要使用多个键提取哈希元素列表),您应该使用哈希切片.在这种情况下,@eastside只是@{$d}{qw/ ne e se /}

  • You should make use of hash slices in situations like this where you need to extract a list of hash elements using multiple keys. In this case @eastside is just @{$d}{qw/ ne e se /}

Perl允许省略在成对的括号和括号之间的间接运算符->,因此可以将$d->{ne}->{data}编写为$d->{ne}{data}

Perl allows the indirection operator -> between pairs of closing and opening braces and brackets to be omitted, so $d->{ne}->{data} can be written $d->{ne}{data}

您正在打印join的结果,其中元素之间为空.仅列出要打印的项目,即可得到相同的结果.您还可以将哈希元素插值到双引号字符串中,因此"\t".$_->{data}."\n""\t$_->{data}\n"

You are printing the result of a join with a null between the elements. You'll get the same result by just listing the items to be printed. You can also interpolate hash elements into a double-quoted string, so "\t".$_->{data}."\n" is the same as "\t$_->{data}\n"

在工作程序中进行这些更改

Making those changes results in this working program

use strict;
use warnings;

# A set of data
my $design = {
   box => {
      ne => {data => 'north-east'},
      nw => {data => 'north-west'},
      n  => {data => 'north'},
      s  => {data => 'south'},
      e  => {data => 'east'},
      w  => {data => 'west'},
      se => {data => 'south-east'},
      sw => {data => 'south-west'},
   }
};

# Select a design
my $selected = 'box';

# Build some arrays
my $d = $design->{$selected};

my @eastside  = @{$d}{qw/ ne e se /};
my @westside  = @{$d}{qw/ nw w sw /};
my @northside = @{$d}{qw/ nw n ne /};

# Update one item
$d->{ne}{data} .= " updated!";

# Display
print "Composed of:\n"; 
print "   $_->{data}\n" for @eastside;

输出

Composed of:
   north-east updated!
   east
   south-east

这篇关于在Perl中使用数据引用的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆