2

This is a design setup question. I know in Perl there are not array of arrays. I am looking at reading in code that pulls in data from large text files at phases of something in flight. Each of these phases track different variables (and different numbers of them) . I have to store them because in the second part of the script i am rewriting them into another file I am updating as I read in.

I thought first I should have an array of hash's, however the variables are not the same at each phase. Then I thought maybe and array with the name of several arrays (array of references I guess) .

Data example would be similar to

phase 100.00  mass 0.9900720175260495E+005 
phase 240.00  gcrad 61442116.0 long 0.963710076E+003 gdalt 0.575477727E+002 vell 0.9862937759999998E+002

Data is made up but you should get the idea and there would be many phases and the variable would likely range from 1 to 25 variables in each phase

2
  • does a phase come in more than once? do the position of the variables within a phase have meaning? What is the scale of the data, hashes and arrays are limited by RAM... Commented Jul 16, 2014 at 20:22
  • 1
    harschware - the phase only happens once. Its sort of like lift off, landing ect. The data are independent variables out to the 15th decibel so I don't think ram is an issue. Sorry I should have explained phase a little better. Commented Jul 17, 2014 at 12:10

2 Answers 2

3

You can use Arrays of Arrays in Perl. You can find documentation on Perl data structures including Arrays of Arrays here: http://perldoc.perl.org/perldsc.html. That said, looking at the sample you've provided it looks like what you need is an Array of Hashes. Perhaps something like this:

my @data = (
  { phase => 100.00,
    mass  => 0.9900720175260495e005 },
  { phase => 240.00
    gcrad => 61442116.0
    long  => 0.963710076e003
    gdalt => 0.575477727e002
    vell  => 0.9862937759999998e002 }
);

to access the data you would use:

$data[0]->{phase} # => 100.00

You could also use a Hash of Hashes like this:

my %data = (
  name1 => { 
    phase => 100.00,
    mass  => 0.9900720175260495e005
  },
  name2 => {
    phase => 240.00
    gcrad => 61442116.0
    long  => 0.963710076e003
    gdalt => 0.575477727e002
    vell  => 0.9862937759999998e002
  }
);

to access the data you would use:

$data{name1}->{phase} # => 100.00

A great resource for learning how to implement advanced data structures and algorithms in Perl is the book, Mastering Algorithms in Perl

Sign up to request clarification or add additional context in comments.

2 Comments

drnewman, Thanks. I was leaning to Array of hashes I was concerned because all the examples I saw seemed to have uniformed subdata which my data does not. Thanks for the clear example and the advance ref. book!
No problem, I've been there before.
2

I use the following mnemonic when defining arrays, array references and hash references:

Use parentheses for lists -- lists can be assigned to either arrays or hashes:

my %person = (
    given_name  => 'Zaphod',
    surname     => 'Beeblebrox'
);

or

my @rainbow = (
    'red',
    'orange',
    'yellow',
    'green',
    'blue',
    'indigo',
    'violet'
);

Because the lists are assigned to list types -- array and hash, there is no semantic ambiguity. When you deal with array references or hash references, however, the delimiter must distinguish between the types of reference, because the $ sigil for scalar variables can't be used to distinguish between the two types of reference. Therefore, [] is used to denote array references, just as [] is used to dereference arrays, and {} is used to denote hash references, just as {} is used to dereference hashes.

So an array of arrayrefs looks like this:

my @AoA = ( 
   [1,2,3],
   [4,5,6],
   [7,8,9]
);

An array of hashrefs:

my @AoH = (
      { given_name => 'Ford',   surname => 'Prefect' },
      { given_name => 'Arthur', surname => 'Dent' }

);

A hashref assigned to a scalar variable:

my $bones = {
    head        => 'skull',
    jaw         => 'mandible',
    'great toe' => 'distal phalanx'
};

1 Comment

carole, feel free to up-vote :-). (You can only accept one answer, but you can vote up any that you feel are good).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.