Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Returning more than two arrays to the subroutine

by shroh (Acolyte)
on Aug 18, 2015 at 16:29 UTC ( [id://1139043]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Experts, I have a requirement which leads me to return more than two arrays to the subroutine which is called from the main subroutine. a) array @nodelist is holding all the server names on which i am running some actions using the script. b) array @errorlist is holding all the server names on which actions failed. From getting the number of elements in @errorlist i need to finally spit in the logs the total number of servers which failed out of total. Below is the code snippet which i am using to return 2 arrays. But somehow this is not working. Instead of passing the array itself i am passing the reference to the array which is also giving some memory errors. I am not sure why. Any ideas are highly appreciated.
sub main { my $path = 'E:/scripts/OutageNodes/'; require $path.'omwNodeDetails.pm'; open_log($path.'maintenanceMode_'); my $mode=get_mode(); my ($nodelist,$errorlist) = get_node($path.'serverlist.txt'); if( $mode eq 'enable'){ enable_unplanned_outage(@nodes); } elsif($mode eq 'disable'){ disable_unplanned_outage(@nodes); } else { printLog("Error invalid Mode $mode"); } # clear serverlist printLog ("Truncating serverlist"); open( SRV,'>',$path.'serverlist.txt') or die "Can't open SRV '$path.serverlist': $!"; close SRV; close_log(); printLog("there were ".scalar @$errorlist." errors in serverlist"); } sub get_node { my ($infile) = @_; my @nodelist = (); my @errorlist = (); ##checks if the file exists and pulls the info out if (-e $infile){ open INFILE, '<', $infile or die "Could not open $infile : $!"; print_log("Scanning $infile"); while (my $node = <INFILE>){ chomp($node); my ($hostname) = split /\./, $node; my $fqdn = getNodeAttributes($hostname,'PrimaryNodeName'); if (length($fqdn) < 1) { print_log("No value returned from WMI, node ($node) doesn't ex +ists in OMW."); push @errorlist,$node; } else { print_log("$node => $hostname => $fqdn"); push @nodelist,$fqdn; } } close INFILE; } else { print_log("ERROR Cannot open $infile"); } return (\@nodelist,\@errorlist); }

Replies are listed 'Best First'.
Re: Returning more than two arrays to the subroutine
by choroba (Cardinal) on Aug 18, 2015 at 16:35 UTC
    which is also giving some memory errors
    What errors?

    How do you populate @nodes? Maybe the following line is missing after the call to get_node:

    my @nodes = @$nodelist;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Returning more than two arrays to the subroutine
by GotToBTru (Prior) on Aug 18, 2015 at 16:37 UTC

    Your syntax seems to be correct in terms of passing the references back, but it would really help if instead of saying you're getting "some memory errors" if you would be explicit. What errors? Also, if you could simplify the program down to the minimum to demonstrate the problem, it would help as well.

    You could also use Data::Dumper to verify the structure of @nodelist within your subroutine.

    Dum Spiro Spero
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139043]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found