Perl - 返回一组哈希值

时间:2012-08-27 07:34:09

标签: perl

我有一系列要返回的哈希值。

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

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

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

谢谢...:)

#!/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
}

2 个答案:

答案 0 :(得分:2)

Perl不是C,原型是针对非常不同和特殊的东西。如果你不知道他们服务的利基目的,那么从不使用它们

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

如果要将数组声明为空,则也没有理由初始化数组。这就是Perl默认做的事情

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

正如其他人所说,您将引用返回到数组。但是,如果您将其作为参考并将其作为参考使用,则可能更好地取消引用返回值。唯一必要的更改是迭代返回的数组

这是您的计划的更规范形式,我希望能帮助

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

答案 1 :(得分:1)

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

return \@dotNetHashArray

你必须

@one = @{ get_array_Hashes() }; 

取消引用它。

另外

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

  • 通常您不需要在Perl中使用原型(参见Why are Perl 5's function prototypes bad?

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

  • 您不需要游标变量来迭代Perl中的数组

    for my $item (@dotNetHashArray) {
        print "\n Hash Value: $item->{Version}";
    }
    
  • 如果您需要在打印开头设置\n,那么在循环之后您将错过\n

你最终会得到:

#!/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"; 
}