Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have created a complex data structures that works perfectly for my task but don't really know how to even call it. There is a reason I did it that way and not another but can someone tell me what monster I have created? Simplified code with easier variable names below.

my %Data; my $x = 0; foreach my $Name (@Names) { push @{$Data{$Name}->{Rank}}, $Dataset[$x]; push @{$Data{$Name}->{Salary}}, $Dataset[$x]; etc.. }

Btw is there any set asthetic 'rule' when you are creating a software when it comes to loops layout? I mean shoud there be a one line space after the if/foreach and one line space after the last line of code and closing '}'? The code is getting quite large even with savings subs on separate files but I don't want to it look to clutered. thanks for your help.

Replies are listed 'Best First'.
Re: Complex Data Structure
by Neighbour (Friar) on May 21, 2012 at 14:49 UTC
    Well, by the looks of it, it's a hash (%Data) which references another hash (with keys Rank and Salary), which references an array (where you push your datasets into). Depending on what's *in* that dataset it could go on, but for now you have a hash-of-hashref-of-arrayref :)
    You could make it more elegant and use a hashref to start with (so use $Data->{$Name} instead of $Data{$Name} and don't initialise it with my %Data but my $Data) in which case you'll end up with a hashref-of-hahref-of-arrayref (or HRHRAR :P)

    As for the aestetics, there's a program for that (there usually is) called perltidy. It has more rules than you will initially care for, but once you've got your personally approved configuration set up, you can convert all perl codefiles that come your way to the layout you prefer.
      Great exactly what i was looking for. One thing - why should I initilise it with $Data and not %Data?

        No reason I'm aware of; just a matter of personal preference. I tend to use a hash variable rather than a hashref at the top level to save typing arrows ( $data{level1}{level2} instead of $data->{level1}{level2} ) unless it's a reference to an object or I'm going to be passing it as a reference to a lot of functions or something, in which case I'll save a little typing by passing $data instead of \%data. So it really just depends on how I'll be using it.

        Aaron B.
        Available for small or large Perl jobs; see my home node.

        Stupid question I get it.. Thanks
Re: Complex Data Structure
by kennethk (Abbot) on May 21, 2012 at 14:48 UTC

    How can I visualize my complex data structure?, short answer Data::Dumper

    In order to retrieve data, you might use something like my $value = $Data{$Name}{Rank}[$i], though others on this site would say you should always include the explicit dereference, as in my $value = $Data{$Name}->{Rank}->[$i].

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Complex Data Structure
by Anonymous Monk on May 21, 2012 at 14:39 UTC
    Ow just to avoid confusion that should be $Dataset_1[$x] and $Dataset_2[$x].