second question today. eak. use array, hash or somthing else?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am troubled.

As you will have seen from my last post I am building myself a
timesheets system in Perl. I am reading in tasks from a file, and
would like to filter then so they relate to a specific job (via
job-number), then sorting them by date done (a job may well have quite
a few tasks done on the same day).

I can't use a 2d array, because each task has many fields.

I can't use a hash of arrays with dates as keys are there will be

Which brings me to 3d arrays... I can't for the life of me work out
how to sort them. I've looked at PerlFAQ and perdoc perllol, but
appear not to be able to find a solution. Is my brain just feeble?

This is obviously linchpin to the whole bloomin' thing. I've been
thinking for hours trying to work out code to sort a 3D array, but
can't do it. Arg! This must have been done before. I take it that you
can't sort a hash of arrays by one of the array values!?!

Please help me. Please. You're all better people than me.

Thanks in advance.

Daniel V

Re: second question today. eak. use array, hash or somthing else?

Quoted text here. Click to load it

What have you tried so far, what didn't work?

Quoted text here. Click to load it

A hash of arrays is not a 3D array.  Which structure are you actually

use strict;
use warnings;
my %HoA = (
     'foo' => [qw/1 gamma M/],
     'bar' => [qw/4 alpha Z/],
     'baz' => [qw/2 omega A/],

#In each of these sort routines, $a and $b are
#keys of the hash.  The values of the hash
#are array references.
#$HoA[0] could be more explicitly written
#as $HoA->[0]
sub by_num {
  $HoA[0] <=> $HoA[0]

sub by_greek {
  $HoA[1] cmp $HoA[1]

sub by_let {
  $HoA[2] cmp $HoA[2]

foreach my $order (\&by_num, \&by_greek, \&by_let){
  foreach my $key (sort $order keys %HoA){
    print "$key: @}\n";
  print "\n";

use strict;
use warnings;
my @ThreeD = (
               [qw/A1a A1b A1c/],
               [qw/A2a A2b A2c/],
               [qw/A3a A3b A3c/],
               [qw/B1a B1b B1c/],
               [qw/B2a B2b B2c/],
               [qw/B3a B3b B3c/],
               [qw/C1a C1b C1c/],
               [qw/C2a C2b C2c/],
               [qw/C3a C3b C3c/],

#In this sort routine, $a and $b will be two members
#of @ThreeD, that is, two arrayrefs.
#$a->[1] is the 2nd element of the array referenced by $a,
#and $a->[1][1] is the 2nd element of the array referenced by $a->[1]
sub by_center {
  $a->[1][1] cmp $b->[1][1];

foreach my $plane (sort by_center @ThreeD){
  foreach my $row (@$plane){
    print "@$row\n";
  print "\n";
#Here, I visualize the array as a cube, and assume you want to sort
#each 'layer' of the cube by their center elements
#This actually isn't an especially good example, because the elements
#are already sorted.  I'm hoping you can extrapolate.

Hope you find this useful,
Paul Lalli

Site Timeline