Link to home
Start Free TrialLog in
Avatar of jkipp_66
jkipp_66

asked on

combining nested data structure into one array

I have a subroutine that returns 3 array refs. so i have:
my ($stats, $totals, $loads) = gets_stats();
$stats and $totals are reference to arrays of arrays. $loads is just a ref to an array. what i want to do is is combine each "record" of each array into one. here is how the structure look:
$stats -> @array -> [user, cpu, mem]
$totals -> @array -> [tot_cpu, tot_mem]
$loads -> [load1, load2]

so i would like to itereate thru each of these array and end up with
@stats = [ user, cpu, mem, tot_cpu, tot_mem, load1, load2]
         [ ..another record...]
         [ ..another record...] ...etc....

any ideas? please let me know if you need further explanation of this question. thanks




 
Avatar of rj2
rj2

You can use an anonymous hash as shown in sample below.

#!/usr/bin/perl
use strict;

my @stats;

my($statref)=get_stats();


foreach(@$statref) {
     printf("CPU: %s, MEM: %ul\n",$_->{CPU},$_->{MEM});
}    

sub get_stats {
     my(@tempstat);
     $tempstat[0]=get_single_stat();    
     $tempstat[1]=get_single_stat();    
     return \@tempstat;
}    

sub get_single_stat {
     my($record) = {
     USER => "Jason",
     CPU => "P4 5GHZ",
     MEM => 1073741824,
     TOT_CPU => 1,
     TOT_MEM => 4294967296,
     LOAD1 => 1,
     LOAD2 => 2,
     };
     return $record;
}

ASKER CERTIFIED SOLUTION
Avatar of skolupaev
skolupaev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial