简体   繁体   中英

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.

plz find the below code for referrence.. and do let me know how to read/ return an array of hashes

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. 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

There is also no reason to initialise arrays when you declare them if you want them empty. That is what Perl does by default

People familar with Perl would thank you for using lower-case and underscore identifiers for variables and subroutines. Camel case is usually reserved for package names

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? )

  • 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

     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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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