2012-08-27 121 views
1

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

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

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

PLZ找到referrence下面的代码..而且不要让我知道如何读/返回哈希

由于数组... :)

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

您返回对数组的引用,而不是数组。 –

+0

然后你的打印值的方式也不起作用,你在某处省略了一个循环。 –

+0

侧面。如果你是5.10或更高版本,我宁愿打印“\ n”,特别是如果\ n在行首 – justintime

回答

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 
+0

thanxs很多u' r回复它真的帮助我理解参考/取消参考...:)\ – user1335978

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"; 
} 
+0

没有反对downvotes,但他们没有真正有用的,没有理由评论... – Matteo