Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

combining nested data structure into one array

Posted on 2002-06-18
2
Medium Priority
?
165 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

610 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