Perl - 如何更新(和访问)存储在散列中的数组中存储的数组? [英] Perl - How do I update (and access) an array stored in array stored in a hash?

查看:233
本文介绍了Perl - 如何更新(和访问)存储在散列中的数组中存储的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我已经做得比我需要的更复杂了,但是我目前正试图存储一个数组,其中包含一个数组,其中包含一个数组,里面包含一个哈希值的Perl。



ie hash - > array - > array

  use strict; 

my%DEVICE_INFORMATION = {}; #global hash
$ b $ sub someFunction(){
my $ key ='name';
my @storage =();

#assume文件在这里为foreach循环正确打开
foreach my $ line(< DATA>){
if(conditional){
my @ ports =();

$ storage [0] ='banana';
$ storage [1] = \ @ ports;
$ storage [2] ='0';

$ DEVICE_INFORMATION {$ key} = \ @ storage;
}

elsif(条件){
push @ {$ DEVICE_INFORMATION {$ key} [1]},5;
}
} #end foreach
} #end someFunction

这是我写的代码的简化版本。我有一个我主要调用的子程序。它解析一个非常特别设计的文件。该文件保证 if语句在后续elsif语句之前触发。



我认为elsif语句中的push调用不能正常工作 - 即5未存储在应存在于@ storage数组中的@ports阵列中,当我将密钥散列到DEVICE_INFORMATION时应返回。



在主要尝试并打印出@storage数组的每个元素来检查事情是否正常运行。

  #main执行
& amp ; someFunction();

print $ DEVICE_INFORMATION {'name'} [0];
print $ DEVICE_INFORMATION {'name'} [1];
print $ DEVICE_INFORMATION {'name'} [2];

此输出结果为... banana ARRAY(blahblah)0



如果我将中间调用的打印语句更改为:

  print @ {$ DEVICE_INFORMATION { '名称'} [1]}; 

或者:

  print @ {$ DEVICE_INFORMATION {'name'} [1]} [0]; 

输出更改为香蕉[blankspace] 0



请告知我如何正确更新@ports数组,并将其存储在已经被散列到DEVICE_INFORMATION中的@ storage数组中,然后如何访问@ports的元素。非常感谢!



我对这篇文章的篇幅感到抱歉。这是我在stackoverflow上的第一个问题。

解决方案

我会告诉你, Data :: Dumper 可以帮助您排序取出Perl数据结构,但 Data :: Dumper 还可以告诉您有关您的第一个问题:



以下是在您打开-curly + close-curly('{}')到一个散列:

  use Data :: Dumper(); 

my%DEVICE_INFORMATION = {}; #global hash
print Dumper->转储([\%DEVICE_INFORMATION],['* DEVICE_INFORMATION']);

以下是输出:

 %DEVICE_INFORMATION =(
'HASH(0x3edd2c)'=> undef
);

您所做的是为您分配字符串哈希 / em>作为它后面的列表元素的关键。

  my%DEVICE_INFORMATION = {} => (); 

所以Perl给它赋值 undef



分配给哈希值时,分配一个列表。字面空的散列不是一个列表,它是一个散列引用。你想为空散列做什么 - 以及什么是完全没有必要的 - 是这样的:

  my%DEVICE_INFORMATION =(); 

这是不必要的,因为它 完全相同: p>

  my%DEVICE_INFORMATION; 

您声明了一个散列,并且该语句将其完全标识为散列。 Perl不会猜测你想要什么,所以它是一个空的散列。



最后,我使用 Data :: Dumper 。如果你开始你的哈希权,并做了以下:

  my%DEVICE_INFORMATION; #= {}; #global hash 
my @ports =(1,2,3);

#注意到我刚刚跳过了@storage
#的临时结构,并将它赋值为文本
#* Perl具有最好的文字数据结构语言之一。
$ DEVICE_INFORMATION {name} = ['banana',\ @ ports,'0'];
print Data :: Dumper->转储(
[\%DEVICE_INFORMATION]
,['* DEVICE_INFORMATION']
);

您看到的是:

 %DEVICE_INFORMATION =(
'name'=> [
'banana',
[
1,
2,
3
],
'0'
]
);

因此,您可以更好地了解它是如何被存储的,以及您必须尊重的级别以及

顺便说一下, Data :: Dumper 可以提供100%的可运行性Perl代码,并向您展示如何指定与文字相同的结构。一个告诫,你必须首先使用 strict (你应该总是使用它)首先声明变量。


Perhaps I have made this more complicated than I need it to be but I am currently trying to store an array that contains, among other things, an array inside a hash in Perl.

i.e. hash -> array -> array

use strict;

my %DEVICE_INFORMATION = {}; #global hash

sub someFunction() {
    my $key = 'name';
    my @storage = ();

    #assume file was properly opened here for the foreach-loop
    foreach my $line (<DATA>) {
        if(conditional) {
            my @ports = ();

            $storage[0] = 'banana';
            $storage[1] = \@ports;
            $storage[2] = '0';

            $DEVICE_INFORMATION{$key} = \@storage;      
        }

        elsif(conditional) {
            push @{$DEVICE_INFORMATION{$key}[1]}, 5;
        }
    }#end foreach
} #end someFunction

This is a simplified version of the code I am writing. I have a subroutine that I call in the main. It parses a very specifically designed file. That file guarantees that the if statement fires before subsequent elsif statement.

I think the push call in the elsif statement is not working properly - i.e. 5 is not being stored in the @ports array that should exist in the @storage array that should be returned when I hash the key into DEVICE_INFORMATION.

In the main I try and print out each element of the @storage array to check that things are running smoothly.

#main execution
&someFunction();

print $DEVICE_INFORMATION{'name'}[0];
print $DEVICE_INFORMATION{'name'}[1];
print $DEVICE_INFORMATION{'name'}[2];

The output for this ends up being... banana ARRAY(blahblah) 0

If I change the print statement for the middle call to:

print @{$DEVICE_INFORMATION{'name'}[1]};

Or to:

print @{$DEVICE_INFORMATION{'name'}[1]}[0];

The output changes to banana [blankspace] 0

Please advise on how I can properly update the @ports array while it is stored inside the @storage array that has been hash'd into DEVICE_INFORMATION and then how I can access the elements of @ports. Many thanks!

P.S. I apologize for the length of this post. It is my first question on stackoverflow.

解决方案

I was going to tell you that Data::Dumper can help you sort out Perl data structures, but Data::Dumper can also tell you about your first problem:

Here's what happens when you sign open-curly + close-curly ( '{}' ) to a hash:

use Data::Dumper ();

my %DEVICE_INFORMATION = {}; #global hash
print Dumper->Dump( [ \%DEVICE_INFORMATION ], [ '*DEVICE_INFORMATION ' ] );

Here's the output:

%DEVICE_INFORMATION  = (
                         'HASH(0x3edd2c)' => undef
                       );

What you did is you assigned the stringified hash reference as a key to the list element that comes after it. implied

my %DEVICE_INFORMATION = {} => ();

So Perl assigned it a value of undef.

When you assign to a hash, you assign a list. A literal empty hash is not a list, it's a hash reference. What you wanted to do for an empty hash--and what is totally unnecessary--is this:

my %DEVICE_INFORMATION = ();

And that's unnecessary because it is exactly the same thing as:

my %DEVICE_INFORMATION;

You're declaring a hash, and that statement fully identifies it as a hash. And Perl is not going to guess what you want in it, so it's an empty hash from the get-go.

Finally, my advice on using Data::Dumper. If you started your hash off right, and did the following:

my %DEVICE_INFORMATION; # = {}; #global hash    
my @ports = ( 1, 2, 3 );

# notice that I just skipped the interim structure of @storage
# and assigned it as a literal 
# * Perl has one of the best literal data structure languages out there.
$DEVICE_INFORMATION{name} = [ 'banana', \@ports, '0' ];
print Data::Dumper->Dump( 
      [ \%DEVICE_INFORMATION ]
    , [ '*DEVICE_INFORMATION' ] 
    );

What you see is:

%DEVICE_INFORMATION  = (
                         'name' => [
                                     'banana',
                                     [
                                       1,
                                       2,
                                       3
                                     ],
                                     '0'
                                   ]
                       );

So, you can better see how it's all getting stored, and what levels you have to deference and how to get the information you want out of it.

By the way, Data::Dumper delivers 100% runnable Perl code, and shows you how you can specify the same structure as a literal. One caveat, you would have to declare the variable first, using strict (which you should always use anyway).

这篇关于Perl - 如何更新(和访问)存储在散列中的数组中存储的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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