简体   繁体   English

Perl - 返回一组哈希值

[英]Perl - Return an array of hashes

i have an array of hashes to be returned. 我有一系列哈希要返回。

before returning the array i cross checked it. 在返回数组之前我交叉检查它。 it was working fine. 它工作正常。

but after returning the array of hashess to the calling sub, i am not able to read it. 但在将hashess数组返回给调用子后,我无法读取它。

plz find the below code for referrence.. and do let me know how to read/ return an array of hashes plz找到以下代码参考..并让我知道如何读取/返回哈希数组

Thanks... :) 谢谢... :)

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

Perl isn't C, and prototypes are meant for something very different and special. Perl不是C,原型适用于非常不同和特殊的东西。 If you don't know what niche purpose they serve then never use them 如果您不知道他们服务的利基目的,那么永远不要使用它们

Similarly there is no reason to pre-declare a subroutine before calling it. 类似地,没有理由在调用它之前预先声明子例程。 As long as you don't use prototypes Perl will do the right thing 只要您使用原型,Perl就会做正确的事情

There is also no reason to initialise arrays when you declare them if you want them empty. 如果要将数组声明为空,则也没有理由初始化数组。 That is what Perl does by default 这就是Perl默认做的事情

People familar with Perl would thank you for using lower-case and underscore identifiers for variables and subroutines. 熟悉Perl的人会感谢你为变量和子程序使用小写和下划线标识符。 Camel case is usually reserved for package names Camel case通常保留用于包名

As others have said, you are returning a reference to an array. 正如其他人所说,您将返回对数组的引用 But instead of dereferencing the return value it is probably better if you keep it as a reference and use it as such. 但是,如果您将其作为参考并将其作为参考使用,则可能更好地取消引用返回值。 The only change necessary is to iterate over the array that is returned 唯一必要的更改是迭代返回的数组

Here is a more canonical form of your program which I hope will help 这是一个更规范的程序形式,我希望对此有所帮助

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
}

output 产量

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

You are returning a reference to an array: 您正在返回对数组的引用:

return \@dotNetHashArray

you have to 你必须

@one = @{ get_array_Hashes() }; 

to dereference it. 取消引用它。

In addition 此外

  • the // comment will not work (use # ) //注释不起作用(使用#

  • usually you don't need to use prototypes in Perl (see Why are Perl 5's function prototypes bad? ) 通常你不需要在Perl中使用原型(参见为什么Perl 5的函数原型不好?

  • you will need a loop also after the return to print out the values 返回后还需要一个循环来打印出值

  • you don't need a cursor variable to iterate over arrays in Perl 你不需要一个游标变量来迭代Perl中的数组

     for my $item (@dotNetHashArray) { print "\\n Hash Value: $item->{Version}"; } 
  • if you need to have the \\n at the beginning of your prints you are a missing a \\n after the loop 如果你需要有\\n在你打印的开头你是一个缺少\\n循环后

You will end up with: 你最终会得到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM