http://www.perlmonks.org?node_id=1059334

nramya82 has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I am very beginner to perl and I am trying to change one existing script to update new API. I have very complicated datastructure which i need to merge and have to print only required fields. But when I merge I get only numeric value. Can some one please suggest any idea? My code:

my @member_lists = @{$soapResponse->result}; my @memberobjectstatus = @{$soapResponse->result}; my @status = push (@member_lists,@memberobjectstatus); use Data::Dumper; print Dumper(@status); orginal @member_lists = $VAR1 = bless( [ bless( { 'address' => '/Common/10.116 +.38.51', 'port' => '80' }, 'Common::AddressPort' ), bless( { 'address +' => '/Common/10.116.38.52', 'port' => '80' }, 'Common::AddressPort' +) ], 'Common::AddressPort[]' ); @memberobjectstatus = @ $VAR2 = bless( [ bless( { 'availability_status' => 'AVAILABILITY_STA +TUS_GREEN', 'status_description' => 'Pool member is available', 'enab +led_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ), + bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'stat +us_description' => 'Pool member is available', 'enabled_status' => 'E +NABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], 'LocalLB::Obje +ctStatus[]' );
VAR1 = 2; I am not sure what I am doing worng here. I need @status which to have all the data so that I can sort out keys and print address, port,availability.
Like this $VAR1 = bless( [ bless( { 'member' => bless( { 'address' => '192.150.1 +6.166', 'port' => '80' }, 'Common::IPPortDefinition' ), 'state' => 'S +TATE_ENABLED' }, 'GlobalLB::PoolMember::MemberEnabledState' ) ], 'Glo +balLB::PoolMember::MemberEnabledState[]' );

Replies are listed 'Best First'.
Re: Complicated datastructre and after merging giving numeric value need help!!
by kennethk (Abbot) on Oct 23, 2013 at 19:03 UTC
    my @status = push (@member_lists,@memberobjectstatus);

    From push:

    • push ARRAY,LIST
    • push EXPR,LIST
    Treats ARRAY as a stack by appending the values of LIST to the end of ARRAY. The length of ARRAY increases by the length of LIST... Returns the number of elements in the array following the completed push.

    push changes the array and returns the length of the array. What you probably mean is:

    my @member_lists = @{$soapResponse->result}; my @memberobjectstatus = @{$soapResponse->result}; my @status = @member_lists; push (@status,@memberobjectstatus); use Data::Dumper; print Dumper(@status);

    This copies the list, and then updates it.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thank you for your response. Yes it worked but when I tried to print each value I am just getting hash reference and not the value.

      Below is my code in .pm file I need to produce output like this
      '/Common/10.116.38.51', 10.116.38.51:1935, enabled:ENABLED, avail +ability:GREEN '/Common/10.116.38.52', 10.116.38.52:1935, enabled:ENABLED, availa +bility:GREEN
      from the this
      print Dumper(@status); $VAR1 = bless( [ bless( { 'address' => '/Common/10.116.38.51', 'port' +=> '80' }, 'Common::AddressPort' ), bless( { 'address' => '/Common/10 +.116.38.52', 'port' => '80' }, 'Common::AddressPort' ) ], 'Common::Ad +dressPort[]' ); $VAR2 = bless( [ bless( { 'availability_status' => 'A +VAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is av +ailable', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::O +bjectStatus' ), bless( { 'availability_status' => 'AVAILABILITY_STATU +S_GREEN', 'status_description' => 'Pool member is available', 'enable +d_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], + 'LocalLB::ObjectStatus[]' );

      My mail .pm files with has the below code

      </code>
      sub locallb_get_member_v2 {
      my $ENABLED_STATUS_MAP = { "ENABLED_STATUS_NONE" => "NONE", "ENABLED_STATUS_ENABLED" => "ENABLED", "ENABLED_STATUS_DISABLED" => "DISABLED", "ENABLED_STATUS_DISABLED_BY_PARENT" => "DISABLED_BY_PARENT", };
      my $AVAILABILITY_STATUS_MAP = { 'AVAILABILITY_STATUS_NONE' => 'Error scenario', 'AVAILABILITY_STATUS_GREEN' => 'GREEN', 'AVAILABILITY_STATUS_YELLOW' => 'YELLOW', 'AVAILABILITY_STATUS_RED' => 'RED', 'AVAILABILITY_STATUS_BLUE' => 'BLUE', 'AVAILABILITY_STATUS_GRAY' => 'GRAY', };
      push (@status,@memberobjectstatus); my %members;
      foreach my $member (@status){ $members{ $status->{'address'} = $member->{'port'}}; $members{ $status}->{'enabled'} = $ENABLED_STATUS_MAP->{ $status->{'enabled_status'} } ; $members{ $status}->{'availability'} = $AVAILABILITY_STATUS_MAP->{ $status->{'availability_status'} } ;
      return \%members; }
      </code>

      In main cgi i have the below code

      my $membershref = $bigip->locallb_get_member_v +2( $bigip_host, $pool ); if ( !$membershref ){ printError("Failed to find mem +bers for $pool"); } else { # if ( !$membershref ); print '<UL>'; + # Pool Members foreach my $member ( sort keys + %{$membershref} ) { print li( "'$member', $membershref->{$member}->{'address'}:$membershref->{$membe +r}->{'port'}, enabled:$membe +rshref->{$member}->{'enabled'}, availability:$memb ershref->{$member}->{'availability'}" ); } print '</UL>';

      But the above code produce below output

      'HASH(0xbafd04)', :, enabled:, availability:

        Your code in sub locallb_get_member_v2 { constructs a weird data structure and contains stuff that makes it hard to believe this is meant as such:

        $members{ $status->{'address'} = $member->{'port'}};

        Are you sure that this code is intended as such? Did you maybe mean the following? Note the placement of curly braces

        $members{ $status->{'address'}} = $member->{'port'};

        Also, are you using the strict pragma? I can't find where you declare $status, but you use it in the next line:

        $members{ $status}->{'enabled'} = $ENABLED_STATUS_MAP->{ $status->{' +enabled_status'} }

        If your problem is that you are unclear about where parentheses go when assigning things to hashes, I recommend using intermediate variables to store the elements and use sequences of very simple assigments. What key in %members did you want to assign to? $members{ $status } or $members{ $status->{'enabled'} }?

        I recommend restructuring all your hash assignments like the following structure:

        # Restructure the following line # $members{ $status}->{'enabled'} = $ENABLED_STATUS_MAP->{ $status-> +{'enabled_status'} } # ... into lines that end up $members{ $key }= $value; my $key= $status; my $value= $ENABLED_STATUS_MAP->{ $status->{'enabled_status'} }; warn "Assigning '$key'='$value' in results"; $members{ $key }= $value;

        If you read your code like that, it seems highly unlikely to me that you really meant that. Maybe you meant the following?

        # $members{ 'enabled' }= $ENABLED_STATUS_MAP->{ $status->{'enabled_s +tatus'} } # ... into lines that end up $members{ $key }= $value; my $key= 'enabled'; my $value= $ENABLED_STATUS_MAP->{ $status->{'enabled_status'} }; warn "Assigning '$key'='$value' in results"; $members{ $key }= $value;