On 9 Jun 2006 at 11:55, Peter Cornelius wrote:

> The problem you're having is that you're creating a new hash  
> reference in each of your if/elsif statements.  I'm going to make 
> some assumptions about your data since I don't know anything about 
> it, but it looks like you want to group back/home/out times by day.  
> If that's what you want then you've got 2 choices.
> 
> 1) Go the route you're on and scant the array each time for the 
> record you want to update 2) Use a hash of hash refs so you can jump
> straight to the record.
> 
> Not knowing anything else about your app I would go with the second, 
> which might look something like this (I haven't tried to run this):
> 
> my %times;  #<--- hash instead of an array
> my ($i,$key,$day,$dom,$mon,$time,$hour,$week_starting);
> #my $times; #<---- Don't need the intermediate var anymore
> while (defined($i = <FH>)) {
>           next if ($i !~ /^(x|j|k|z)/);
>           chomp($i);
> 
>           ($key,$day,$dom,$mon,$time,$hour) = ($i =~
> /^(\w)\s+(\w+)\s+(\d+)-(\w+)-\d+\s+(\d+:\d+):.*(\d+:\d+|-\d+:-\d+)/);
>           my $colour = 'black';
> 
> 
>           if ($key =~ /x/i ) {
>                   $times{$day . '-' . $dom } = {
>                           day     => $day,
>                           dom     => $dom,
>                           morning => $time,
>                   };
>           }
>           elsif ($key =~ /j/ ) {
>                   $times{$day . '-' . $dom } = { out => $time};
>           }
>           elsif ($key =~ /k/) {
>                   $times{$day . '-' . $dom }  = { back => $time};
>           }
>           elsif ($key =~ /z/) {
>                   $times{$day . '-' . $dom }  = { home => $time};
>           }
>           #push(@times,$times); #<---- Don't need to push anymore 
> since it went straight onto the hash.
>   }
> >
> 
> I'm using the $day-$dom key but that's probably not unique enough.  
> You probably want the full date as the key.
> 
> Hope this helps,

Thanx. I opted for John's suggestion. Peter's solution would no doubt 
be equally effective.

The data is to be parsed by Template-Toolkit and I am a little un-
clear about how to loop through a hash of hashes in TT.

Again thank you both for taking the time. Much appreciated.
Dp.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to