1

我有一个要返回的哈希数组。

在返回数组之前,我交叉检查了它。它工作正常。

但是在将哈希数组返回给调用子之后,我无法读取它。

请找到以下代码以供参考..并让我知道如何读取/返回哈希数组

谢谢... :)

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

# Subroutine prototypes
sub get_two_arrays();


my @one=();
@one = get_array_Hashes();
print "\n First: @one->{Version}\n";  // Printing the return array of hashes


sub get_array_Hashes() {



my @dotNetHashArray =();

    my $dotNetHash1 = {Version => "Test-1 Version", SP => "installedSp", Build => "installedBuild"};                                
    push @dotNetHashArray, $dotNetHash1;

    my $dotNetHash2 = {Version => "Test-2 Version", SP => "installedSp", Build => "installedBuild"};                                
    push @dotNetHashArray, $dotNetHash2;

    my $dotNetHash3 = {Version => "Test-3 Version", SP => "installedSp", Build => "installedBuild"};                                
    push @dotNetHashArray, $dotNetHash3;


    print "Test Array of hashes before return";
    for(my $i=0; $i<@dotNetHashArray; $i++)
    {
        print("\n Hash Value : ".$dotNetHashArray[$i]->{Version});
    }


    return \@dotNetHashArray
}
4

2 回答 2

2

Perl 不是 C,原型是为一些非常不同和特殊的东西而设计的。如果你不知道它们的用途是什么,那就永远不要使用它们

同样,没有理由在调用子程序之前预先声明它。只要你使用原型 Perl 就会做正确的事

如果您希望它们为空,则在声明它们时也没有理由初始化数组。这就是 Perl 默认所做的

熟悉 Perl 的人会感谢您为变量和子例程使用小写和下划线标识符。驼峰大小写通常是为包名保留的

正如其他人所说,您正在返回对数组的引用。但是,与其取消引用返回值,不如将其保留为引用并照此使用它可能会更好。唯一需要的更改是遍历返回的数组

这是您程序的更规范形式,希望对您有所帮助

use strict;
use warnings;

my $one = get_array_Hashes();
print "\nArray of hashes after return\n";
print "First: $_->{Version}\n" for @$one;

sub get_array_Hashes {

    my @dotnet_hash_array;

    my $dotnet_hash1 = {
        Version => "Test-1 Version",
        SP => "installedSp",
        Build => "installedBuild"
    };                                
    push @dotnet_hash_array, $dotnet_hash1;

    my $dotnet_hash2 = {
        Version => "Test-2 Version",
        SP => "installedSp",
        Build => "installedBuild"
    };                                
    push @dotnet_hash_array, $dotnet_hash2;

    my $dotnet_hash3 = {
        Version => "Test-3 Version",
        SP => "installedSp",
        Build => "installedBuild"
    };                                
    push @dotnet_hash_array, $dotnet_hash3;

    print "Test Array of hashes before return\n";
    for my $i (0 .. $#dotnet_hash_array) {
        print "Hash Value : $dotnet_hash_array[$i]->{Version}\n";
    }

    return \@dotnet_hash_array
}

输出

Test Array of hashes before return
Hash Value : Test-1 Version
Hash Value : Test-2 Version
Hash Value : Test-3 Version

Array of hashes after return
First: Test-1 Version
First: Test-2 Version
First: Test-3 Version
于 2012-08-27T08:18:01.357 回答
1

您正在返回对数组的引用:

return \@dotNetHashArray

你必须

@one = @{ get_array_Hashes() }; 

取消引用它。

此外

  • //评论不起作用(使用#

  • 通常你不需要在 Perl 中使用原型(参见为什么 Perl 5 的函数原型不好?

  • 返回后您还需要一个循环来打印出值

  • 您不需要游标变量来遍历 Perl 中的数组

    for my $item (@dotNetHashArray) {
        print "\n Hash Value: $item->{Version}";
    }
    
  • 如果您需要\n在打印的开头有 a ,那么您\n在循环之后缺少 a

你最终会得到:

#!/usr/bin/perl 

use strict; 
use warnings; 

# do not use function prototypes 
# perl subroutines are usually all lowercase (no camel-case) 
sub get_array_hashes { 

    my @dot_net_hash_array = (); 

    # actually you don't need to create a local variable for each item you push 

    push @dot_net_hash_array, { 

# avoid unncessary string interpolation (use ' if no variables in the string have to be interpolated) 
        version => 'Test-1 Version', 
        sp      => 'installedSp', 
        build   => 'installedBuild' 
    }; 

    push @dot_net_hash_array, 
      { 
        version => 'Test-2 Version', 
        sp      => 'installedSp', 
        build   => 'installedBuild' 
      }; 

    push @dot_net_hash_array, 
      { 
        version => 'Test-3 Version', 
        sp      => 'installedSp', 
        build   => 'installedBuild' 
      }; 

    print "Test Array of hashes before return\n"; 
    for my $item (@dot_net_hash_array) { 
        print "Hash Value :  $item->{version}\n"; 
    } 

    return \@dot_net_hash_array; 
} 

my @one = @{ get_array_hashes() }; 

# Use # for comments 

#  Printing the return array of hashes 
print "Test Array of hashes after return\n"; 
for my $item (@one) { 
    print "Hash Value :  $item->{version}\n"; 
} 
于 2012-08-27T07:43:08.320 回答