Solved

combining nested data structure into one array

Posted on 2002-06-18
2
160 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 300 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl string filter 5 84
create a gui in perl 3 104
Matching a random pattern with one common character 2 86
Export Variables in Perl 3 108
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

726 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