CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Very urgent Complicated datastructre and after merging just giving numeric value

 



nramya82
Novice

Oct 23, 2013, 11:52 AM

Post #1 of 17 (2682 views)
Very urgent Complicated datastructre and after merging just giving numeric value Can't Post

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_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ), bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], 'LocalLB::ObjectStatus[]' );
after merging I simply get
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.16.166', 'port' => '80' }, 'Common::IPPortDefinition' ), 'state' => 'STATE_ENABLED' }, 'GlobalLB::PoolMember::MemberEnabledState' ) ], 'GlobalLB::PoolMember::MemberEnabledState[]' );


BillKSmith
Veteran

Oct 23, 2013, 12:13 PM

Post #2 of 17 (2676 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

This post appears to be a duplicate of one in the beginner's forum. Please post in only one place.
Good Luck,
Bill


nramya82
Novice

Oct 23, 2013, 12:45 PM

Post #3 of 17 (2671 views)
Re: [BillKSmith] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

sorry .. I just deleted the one in beginner section
Thanks


Zhris
Enthusiast

Oct 23, 2013, 7:17 PM

Post #4 of 17 (2653 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Hi,

You probably meant:


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


Perl's push function returns the number of elements in the resultant list. This return value isn't useful in your case.

@memberobjectstatus is being pushed into @members_lists, which is your resultant list.

If you don't want to modify @members_lists, use:


Code
push (@member_lists,@memberobjectstatus);  
my @status = (@member_lists,@memberobjectstatus);


Chris


(This post was edited by Zhris on Oct 23, 2013, 7:20 PM)


nramya82
Novice

Oct 24, 2013, 1:29 AM

Post #5 of 17 (2642 views)
Re: [Zhris] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thanks actually it worked I could get all elements in single array but however when I try to print out the values I am suppose it get value like this but instead I get the hash address.any suggestion what wrong am I doing here
output
IT just returns 'HASH(0xc61b00)', :, enabled:, availability:

Instead of below one
'/Common/10.116.38.51', 10.116.38.51:1935, enabled:ENABLED, availability:GREEN
'/Common/10.116.38.52', 10.116.38.52:1935, enabled:ENABLED, availability:GREEN

I have main .pm program code like below
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', };
my @member_lists = @{$soapResponse->result};
my @memberobjectstatus = @{$soapResponse->result};
my @status = @member_lists;
use data:dumper;

push (@status,@memberobjectstatus);
print dumper (@staus);
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; }
print Dumper(@status)


Main CGI


my $membershref =
$bigip->locallb_get_member_v2( $bigip_host,
$pool );

if ( !$membershref ){
printError("Failed to find members for $pool");
}


else {

# if ( !$membershref );
print '<UL>'; # Pool Members

foreach my $member ( sort keys %{$membershref} ) {
print li(
"'$member', $membershref->{$member}->{'address'}:$membershref->{$member}->{'port'},
enabled:$membershref->{$member}->{'enabled'}, availability:$memb
ershref->{$member}->{'availability'}"
);
}
print '</UL>'; # End Pool members



IT just returns 'HASH(0xc61b00)', :, enabled:, availability:

I tried to add pint statement in the .pm file and trie dto see the object below is what I got
print dumper (@staus);
OUTPUT $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[]' ); $VAR2 = bless( [ bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ), bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], 'LocalLB::ObjectStatus[]' );


Zhris
Enthusiast

Oct 24, 2013, 6:19 AM

Post #6 of 17 (2634 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Hi,

There is a lot wrong with your code, I don't understand how you managed to compile it. Based on what you have provided, here is my re-write, which I cannot properly test:


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

my $member_lists = $soapResponse->result;
my $memberobjectstatus = $soapResponse->result;

my $members = { };

for (0 .. $#{$member_lists})
{
my $address = $member_lists->[$_]->{address};
my ($ip) = $address =~ /\/([^\/]+)$/;
my $port = $member_lists->[$_]->{port};
my $enabled = $ENABLED_STATUS_MAP->{$memberobjectstatus->[$_]->{enabled_status}};
my $availability = $AVAILABILITY_STATUS_MAP->{$memberobjectstatus->[$_]->{availability_status}};

$members->{$address} =
{
ip => $ip,
port => $port,
enabled => $enabled,
availability => $availability
};
}

return $members;
}



Code
my $membershref = $bigip->locallb_get_member_v2( $bigip_host, $pool );  
printError("Failed to find members for $pool") unless (keys %$membershref);

print "<ul>\n";
for my $address ( sort keys %$membershref )
{
my $ip = $membershref->{$address}->{ip};
my $port = $membershref->{$address}->{port};
my $enabled = $membershref->{$address}->{enabled};
my $availability = $membershref->{$address}->{availability};

printf "\t<li>'%s', %s:%s, enabled:%s, availability:%s</li>\n", $address, $ip, $port, $enabled, $availability;
}
print "</ul>\n";


Note, your two calls of $soapResponse->result return blessed lists of blessed hashes, which you probably shouldn't be accessing directly, but I don't know which module you are using to provide a more appropriate suggestion.

Hope this is useful to you.

Chris


(This post was edited by Zhris on Oct 24, 2013, 2:29 PM)


zapzap
User

Oct 24, 2013, 9:48 AM

Post #7 of 17 (2624 views)
Re: [Zhris] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thank you Zhris for making OP code legible. Your version is cleaner and easier to debug than the original.


(This post was edited by zapzap on Oct 24, 2013, 9:55 AM)


nramya82
Novice

Oct 24, 2013, 12:07 PM

Post #8 of 17 (2619 views)
Re: [Zhris] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thank you for your response. Actually I am using F5 incontroller API to get the poolmember address and port using get_member_v2 api and object status using get_object_status which will have blessed list of hashes.

my $soapResponse = $LBPool->get_member_v2(
SOAP::Data->name( pool_names => [$poolname] ) );

my @member_lists = @{$soapResponse->result};

$soapResponse = $LBPool->get_member_object_status(
SOAP::Data->name(pool_names => [$poolname]),
SOAP::Data->name(members => [@member_lists])
my @status = @member_lists;
push (@status,@memberobjectstatus);
print dumper (@status); --- will have all the list like below

$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[]' ); $VAR2 = bless( [ bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ), bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], 'LocalLB::ObjectStatus[]' );

I have to print the output like
'/Common/10.116.38.51', 10.116.38.51:80, enabled:ENABLED, availability:GREEN
'/Common/10.116.38.52', 10.116.38.52:80, enabled:ENABLED, availability:GREEN.

as per your suggestion I tried to change the code and when I compiled the main cgi I see below error

Global symbol &quot;$membersref&quot; requires explicit package name atBigip.cgi . ANy suggestions?

Thank you so much for your help


Zhris
Enthusiast

Oct 24, 2013, 2:18 PM

Post #9 of 17 (2613 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Hi,

I have fixed a couple of variable name errors and have updated my version above. I don't see a "membersref" anywhere, could you provide more information as to which line of code is causing this issue, perhaps you meant "membershref".

Chris


nramya82
Novice

Oct 28, 2013, 4:41 PM

Post #10 of 17 (2576 views)
Re: [Zhris] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thanks lot for your help!!. It improved lot but I am getting error while doing compilation .Can you please help?
Bad index while coercing array into hash at line 605

my $address = $member_lists->[$_]->{address};


Laurent_R
Veteran / Moderator

Oct 29, 2013, 12:43 AM

Post #11 of 17 (2568 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Hmm, difficult to say without seeing more of the context and knowing how the variables are set up, except that Perl expects obviously thinks that $member_lists->[$_] is a reference to an array, not a hash. Do you get an error with this:


Code
my $address = $member_lists->[$_]->[address];


BTW, I would rather expect this type of error to happen during execution rather than compilation, are you sure it happens at compile time? For example, do you get it if you run something like "perl -c program.pl"?


nramya82
Novice

Oct 31, 2013, 10:50 AM

Post #12 of 17 (2493 views)
Re: [Laurent_R] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Sorry for the typo I actually got the error when i tried running the script also my object has the below values
@member_lists = 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 =
bless( [ bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ), bless( { 'availability_status' => 'AVAILABILITY_STATUS_GREEN', 'status_description' => 'Pool member is available', 'enabled_status' => 'ENABLED_STATUS_ENABLED' }, 'LocalLB::ObjectStatus' ) ], 'LocalLB::ObjectStatus[]' );
Its actually blessed hashes


nramya82
Novice

Oct 31, 2013, 3:33 PM

Post #13 of 17 (2483 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

I tried to print the output in same sub routine and i see everything getting printed in same line . I tried to add new line by \n and its not working . Can you please help
for $j (0..$#memberA)
{
$member = $memberA[$j];
my $addr = $member->{"address"};
my $port = $member->{"port"};
my $memberobjectstatus = $memberobjectstatusA[$j];
my $availability_status =$AVAILABILITY_STATUS_MAP->{$memberobjectstatus->{'availability_status'}};
my $enabled_status =$ENABLED_STATUS_MAP->{$memberobjectstatus->{'enabled_status'}};
print "\n ${addr}:${port} \n";
print " : Availability : ${availability_status}\n";
print " : Status : ${enabled_status}\n\n" ;
print "\n";
}

Output

/common/10.116.38.51:80, enabled:ENABLED, availability:GREEN /common/10.116.38.52:80, enabled:ENABLED, availability:GREEN
Instead of
/common/10.116.38.51:80, enabled:ENABLED, availability:GREEN
/common/10.116.38.52:80, enabled:ENABLED, availability:GREEN


Laurent_R
Veteran / Moderator

Nov 1, 2013, 8:54 AM

Post #14 of 17 (2438 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

I have no idea how or why your new line characters are not taken into account. This should not happen with the code you wrote.

Well, actually, two possible ideas:
1. Are you sure you are really executing the code that you have shown? Don't think that I am trying to make fun about you, I have seen that error a number of times: saving the program in one directory and executing another version of it in another directory (or with different names or on different platforms, etc.). So, don't discard the possibility too easily, please really check, really carefully. And don't feel stupid if this is what happened, I have done this error myself and seen every single one of my colleagues doing it. When you change repeatedly something to a program and don't see anything changing in the output when you run it, then the chances that you run something else than the program you are editing are becoming pretty high.

2. The other (but less likely) possibility is that you are running the program one one operating system and view the content on another one, such as running under Linux (or Cygwin) and viewing on Windows, or some other combination. But I consider this less likely because in all cases that I can think of, you would have anomalies on new lines such as things like this:

Code
/common/10.116.38.51:80, enabled:ENABLED, availability:GREEN  
/common/10.116.38.52:80, enabled:ENABLED, availability:GREEN

but I can't think of a case where you would get no new line at all. But if you do plan with different environment, please explain what you do, we should be able to find a remedy.


nramya82
Novice

Nov 1, 2013, 1:12 PM

Post #15 of 17 (2423 views)
Re: [Laurent_R] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thanks for responding . I am actually trying to print this in subroutine and calling this function without changing the main logic . But actually its pulling the correct information and not on separate line. I am really a very beginner and so I dont have knowledge on cgi .. I recently updated this subroutine which working fine. so that of including the new line to print the output in required format ... but now working :-(.

for $j (0..$#memberA)
{
$member = $memberA[$j];
my $addr = $member->{"address"};
my $port = $member->{"port"};
my $memberobjectstatus = $memberobjectstatusA[$j];
my $availability_status =$AVAILABILITY_STATUS_MAP->{$memberobjectstatus->{'availability_status'}};
my $enabled_status =$ENABLED_STATUS_MAP->{$memberobjectstatus->{'enabled_status'}};
print "\n ${addr}:${port} \n";
print " : Availability : ${availability_status}\n";
print " : Status : ${enabled_status}\n\n" ;
print "\n";
}
return \%members;

My main logic:

foreach my $member ( sort keys %{$membershref} ) {
print li(
"'$member', $membershref->{$member}->{'address'}:$membershref->{$member}->{'port'},
enabled:$membershref->{$member}->{'enabled'}, availability:$memb
ershref->{$member}->{'availability'}"

In Reply To


Laurent_R
Veteran / Moderator

Nov 2, 2013, 11:35 AM

Post #16 of 17 (2365 views)
Re: [nramya82] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

So am I understanding correctly that you are writing Web content? If you view the output with a Web browser, you should know that carriage returns are not significant in HTML, you simply won't see them. I haven't done HTML for the last 6 or 7 years, but, as I remember it, you define paragraphs with the <p> and </p> tags, and line breaks with the <br> tag. There are probably some more modern ways of doing these things, but I don't know or don't remember them.


nramya82
Novice

Nov 6, 2013, 11:10 AM

Post #17 of 17 (2277 views)
Re: [Laurent_R] Very urgent Complicated datastructre and after merging just giving numeric value [In reply to] Can't Post

Thank you all for the the quick response. I figured out the issue i replaced <br> with \n and it worked , I see the output in the correct format.

In Reply To

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives