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

D

Dan Vesma

Morning,

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
duplications.

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
 
P

Paul Lalli

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.

What have you tried so far, what didn't work?
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!?!

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

#!/usr/bin/perl
#HASH OF ARRAYS
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{$a}[0] could be more explicitly written
#as $HoA{$a}->[0]
sub by_num {
$HoA{$a}[0] <=> $HoA{$b}[0]
}

sub by_greek {
$HoA{$a}[1] cmp $HoA{$b}[1]
}

sub by_let {
$HoA{$a}[2] cmp $HoA{$b}[2]
}

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

#!/usr/bin/perl
use strict;
use warnings;
#3D ARRAY
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.
__END__


Hope you find this useful,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top