in reply to Hash vs constant vs package vs other for data structure

  1. Use an AoAoH instead, and refer to each data member by name
  2. Use constant declarations to name each index; Use upper-case variables to show their status as constant names for indices
  3. Use a package with setter and getter functions or public data members and use an AoA with the package objects as the contents

The first is probably the way I would go. The second is probably mostly useful if performance is a concern; also it would of course mean you don't have to change your data structure. The third OO approach might be useful if your objects not only have properties, but also methods.

The only thing to take into consideration with the first solution is that you don't have automatic protection against typos in property names like you do in the other two solutions. One possible solution is locked hashes, like I showed here, but note also what I mentioned lower down in that same thread (here) in regards to that they might be deprecated someday.

  • Comment on Re: Hash vs constant vs package vs other for data structure

Replies are listed 'Best First'.
Re^2: Hash vs constant vs package vs other for data structure
by oldtechaa (Beadle) on Mar 27, 2017 at 19:03 UTC
    I don't have methods, so I don't believe I'll go the OO route. It seems like the constant or uppercase methods perform better, are just as readable, and don't make me change my data structure. What are the advantages of the hash method?
      What are the advantages of the hash method?

      Two that I can think of off the top of my head are that you can have sparse property sets (e.g. if one object has {foo=>1,bar=>2} and the other has {quz=>1,baz=>2}, whereas you'd need four array elements to cover that, a bit of a waste), and that textual serializations of the data would be self-documenting. But if neither of those are a concern to you, then at the moment I can't think of major disadvantages to using constants for the array indicies.

        I don't really need either of those, but I also would like to assign to slices, which would be difficult with a hash. I think I'll go with the constant method or uppercase variables probably.
      perl -MData::Dumper -E '$A[3][5][0]="foo"; die Dumper \@A'

      output:

      $VAR1 = [ undef, undef, undef, [ undef, undef, undef, undef, undef, [ 'foo' ] ] ];
      Long? Just try 300 by 500!

      perl -MData::Dumper -E '$A{3}{5}{0}="foo"; die Dumper \%A'

      output:

      $VAR1 = { '3' => { '5' => { '0' => 'foo' } } };

      smaller, but slower to iterate through if you have lots of entries. Still you can use an intermediate variable that acts like a pointer:

      perl -MData::Dumper -E '$A{3}{5}{0}="foo"; $v = $A{3}{5}; $v->{1}="bar +"; die Dumper \%A'

      if you have fixed dimensions... you can also try: $NUM = $x + $y*$WIDTH so if you have 300 pixels wide, (x,y)=(3,5) becomes 3+5*300 = 1503

      but check if you will not run above your maxint: 718414 with that method