0

I'm collecting a bunch of data from multiple servers. I need to store some of the server data and compare against other certain server data. So I don't need an array for each server I'd like to store it in a hash, but I'm having issues actually getting the data in/out of the hash.

foreach my $cluster_type (sort keys %{$href_server_list}){
    foreach my $server (sort @{$href_server_list->{$cluster_type}}){
        my @a_cmd_results = ();
        get_server_data(\@a_cmd_results,$server);
        if($cluster_type eq "TYPEA"){
            push @{$GLOBAL_HASH_TYPEA_DATA{$server}}, @a_cmd_results;
        }
        elsif($cluster_type eq "TYPEB"){
            push @{$GLOBAL_HASH_TYPEB_DATA{$server}}, @a_cmd_results;
        }


my @TYPEA;
my @TYPEB;
my $server;
#loop through the 4 different servers
for(my $i=0;$i<4;$i++){
    $server=$TYPEA_keys[$i];
    push @TYPEA, @{$GLOBAL_HASH_TYPEA_DATA{$server}};#error here, "Cant use an undefined value as an ARRAY reference
    $server=$TYPEB_keys[$i];
    push @TYPEB, @{$GLOBAL_HASH_TYPEB_DATA{$server}};
    #sort and compare data from TYPEA and TYPEB
}

It "works" if I push a reference instead of the array like this

push @{$GLOBAL_HASH_TYPEA_DATA{$server}}, \@a_cmd_results;

But if it's simply referencing the array @a_cmd_results then that's no good because it's continually reused for each server and would only contain the last server?

2 Answers 2

1

You should construct a new array reference in-place using the contents of @a_cmd_results, like this:

push @{$GLOBAL_HASH_TYPEA_DATA{$server}}, [@a_cmd_results];

The [...] syntax in Perl returns an array reference for the list of values inside, as opposed to the (...) syntax which is just a plain list.

Sign up to request clarification or add additional context in comments.

1 Comment

It appears that that is now storing the address of the data and my compare is failing. The values that get put in @TYPEA[0] = "ARRAY(0x2668508) and there is no @TYPEA[1].
0

Seems like push was an issue. While simply trying to assign something equal to an array would typically give you it's length, square brackets changes that. As such the solution was as follows

$GLOBAL_HASH_TYPEA_DATA{$server} = [@a_cmd_results];
my @TYPEA = @{$GLOBAL_HASH_TYPEA_DATA{$server}};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.