?
Solved

combining nested data structure into one array

Posted on 2002-06-18
2
Medium Priority
?
162 Views
Last Modified: 2010-03-05
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




 
0
Comment
Question by:jkipp_66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Expert Comment

by:rj2
ID: 7090133
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;
}

0
 

Accepted Solution

by:
skolupaev earned 1200 total points
ID: 7090758
Hi jkip66,

This is pretty easy if you are certain the arrays
@$stats and @$totals have the same size.   I'd do something like what follows, assuming there is no need to
check on them.    

The main trick to this is getting at the value of an anonymous array, for example $a.  

When $a = [ 'x', 'y', 'z' ], @{$a} gathers all the values in $a and returns a list value ( 'x', 'y', 'z' ).   @$a is a shorthand for @{$a}.

In an array context, @$a returns the list of values.
In a scalar context it returns the number of values in the list.

######### a solution to try ############

@stats=();             # put result here
($stats, $totals, $loads) = &gets_stats();

for ($ix=0; $ix < @$stats; $ix++)

   {
   $stats3 = $stats->[$ix];     # get [usr,cpu,mem]
   $totals2 = $totals->[$ix];   # get [totcpu, totmem]
   push( @stats,  [ @$stats3, @$totals2, @$loads ]  )
   }
   # each iteration pushes a 7-tuple onto @stats:
   # [ usr,cpu,mem,totcpu,totmem,load1,load2]

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question