Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 向数组中添加哈希_Arrays_Perl_Multidimensional Array_Hashtable - Fatal编程技术网

Arrays 向数组中添加哈希

Arrays 向数组中添加哈希,arrays,perl,multidimensional-array,hashtable,Arrays,Perl,Multidimensional Array,Hashtable,我有一个这样的数组 @switch_ports = () 然后要将此散列的50个实例添加到交换机端口数组中 %port = (data1 => 0, data2 => 0, changed => 0) 但是,如果我将哈希推送到数组中 push(@switch_ports, %port) 但如果我打印@switch\u端口 我明白了 data10data20changed0 因此,它似乎只是将它们添加到阵列中,(加入它们) 如果我尝试循环数组并打印密钥,它也会失败 我想我

我有一个这样的数组

@switch_ports = ()
然后要将此散列的50个实例添加到交换机端口数组中

%port = (data1 => 0, data2 => 0, changed => 0)
但是,如果我将哈希推送到数组中

push(@switch_ports, %port)
但如果我打印@switch\u端口 我明白了

data10data20changed0
因此,它似乎只是将它们添加到阵列中,(加入它们) 如果我尝试循环数组并打印密钥,它也会失败

我想我是智障了,因为我的头撞到了桌子上

1-能否在数组中存储哈希

2-你能有一个散列数组吗

试图得到

switchports
    0
        data1
        data2
        changed
    1
        data1
        ....
因此

将返回数组中所有哈希的所有data1

是的,我在Perl中失败了。您可以将对散列的引用存储在数组中:

push @switchport, \%port; # stores a reference to your existing hash

然后迭代,比如

foreach my $port (@switchport) {
    print $port->{'data1'}; # or $$port{'data1'}
}

请参见Perl中的
manperlref

,数组和哈希成员必须是单个值。在Perl5.0之前,没有(简单的)方法来做您想做的事情

但是,在Perl5中,现在可以使用对散列的引用。引用只是存储项的内存位置。要获取引用,请在变量前面加一个反斜杠:

use feature qw(say);

my $foo = "bar";
say $foo;    #prints "bar"
say \$foo;   #prints SCALAR(0x7fad01029070) or something like that
因此:

而且,您不必创建
$port\u ref

my @switch_ports = ();
my %port = ( data1 => 0, data2 => 0, changed => 0 );

push( @switch_ports, \%port );
要获得引用的实际值,只需将符号放回前面:

#Remember: This is a REFERENCE to the hash and not the hash itself
$port_ref = $switch_ports[0];
%port = %{$port_ref};      #Dereferences the reference $port_ref;

print "$port{data1}  $port{data2}  $port{changed}\n";
另一条捷径:

%port = %{$port[0]};   #Dereference in a single step
print "$port{data1}  $port{data2}  $port{changed}\n";
或者,更简短地说,在进行过程中取消引用:

print ${$port[0]}{data1} . " " . ${$port[0]}{data2} . " " . ${$port[0]}{changed} . "\n";
还有一点语法甜味剂。其含义相同,但更易于阅读:

print $port[0]->{data1} . " " . $port[0]->{data2} . " " . $port[0]->{changed} . "\n";
看看Perldoc的和。第一个是教程。

尝试时:

%port = (data1 => 0, data2 => 0, changed => 0);
push @switch_ports, %port;
真正发生的是:

push @switch_ports, "data1", 0, "data2", 0, "changed", 0;
因为数组和散列在列表上下文中使用时将被忽略

当您想要创建50个散列实例时,最好不要像其他人建议的那样使用对现有散列的引用,因为这样只会创建50个对同一散列的不同引用。由于明显的原因,它会崩溃和燃烧

您需要的是:

push @array, { data1 => 0, data2 => 0, changed => 0 } for 1 .. 50;
这将向数组中添加50个唯一的匿名哈希。大括号表示匿名哈希的构造,并返回对它的标量引用

ETA:您关于如何访问此数据的示例是错误的

foreach $port (@switchport) {
    print $port['data1'];    # will use @port, not $port
}
在标量变量上使用下标将尝试访问该命名空间中的数组,而不是标量。在perl中,有两个单独的变量
$port
@port
是有效的。括号用于数组,而不是散列。使用引用时,还需要使用箭头运算符:
$port->{data1}
。因此:

for my $port (@switchport) {
    print $port->{data1};
}

为那些使用此问题的人提供简化,以找到一种通用方法,如标题问题。Mysql主题:

my @my_hashes = ();
my @$rows = ... # Initialize. Mysql SELECT query results, for example.

if( @$rows ) {
    foreach $row ( @$rows ) { # Every row to hash, every hash to an array.
        push @my_hashes, { 
            id => $row->{ id }, 
            name => $row->{ name }, 
            value => $row->{ value }, 
        };
    }
}
要循环并打印,请执行以下操作:

for my $i ( 0 .. $#my_hashes ) {
    print "$my_hashes[$i]{ id }\n ";
    print "$my_hashes[$i]{ name }\n ";
    print "$my_hashes[$i]{ value }\n ";
}


}

Perl 5.000于1994年10月17日发布。我认为,到了2011年,也就是17年后,关于Perl4行为的免责声明不再是非常必要的:)你会认为这是事实。然而,有很多Perl教程仍然停留在古老的Perl 3.x语法中:如果它对爷爷来说足够好,那么对我来说就足够好了!。我仍然看到人们使用
需要“find.pl”
而不是
使用File::Find
切碎
而不是
切碎
。即使是许多现代学习Perl的书籍也从未被引用过。
for my $port (@switchport) {
    print $port->{data1};
}
my @my_hashes = ();
my @$rows = ... # Initialize. Mysql SELECT query results, for example.

if( @$rows ) {
    foreach $row ( @$rows ) { # Every row to hash, every hash to an array.
        push @my_hashes, { 
            id => $row->{ id }, 
            name => $row->{ name }, 
            value => $row->{ value }, 
        };
    }
}
for my $i ( 0 .. $#my_hashes ) {
    print "$my_hashes[$i]{ id }\n ";
    print "$my_hashes[$i]{ name }\n ";
    print "$my_hashes[$i]{ value }\n ";
}
for my $i ( 0 .. $#my_hashes ) {
for my $type ( keys %{ $my_hashes[$i] } ) {
     print "$type=$my_hashes[$i]{$type} ";
}