Solved

combining nested data structure into one array

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now