Hi,
This is a bit of an extension on an earlier post.
I am trying to create a data structure from a file (contents below). It is
meant to be a hash of
hashes but I suspect there is either a typo somewhere or I am hitting some
scoping problems. All
that is left in the hash is the last data assigned to it. Am I overwritting my
earlier assignment?
Data::Dumper shows the data as this
...snip
$VAR3 = 'Wed-07';
$VAR4 = {
'total' => '8:42',
'home' => '17:51'
};
$VAR5 = 'Tue-06';
$VAR6 = {
'total' => '7:20',
'home' => '16:18'
};
$VAR7 = 'Fri-09';
$VAR8 = {
'total' => '8:05',
'home' => '18:37'
};
I want something like this
$VAR3{Wed-07} = {
name => Some Name,
began => Tue-06,
day => Wed,
dom => 07,
morning => 09:27,
home => 18:37,
total => 8:42
};
Can someone point me in the right direction (again)?
########### my effort ##############
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $file = 'myfile.txt';
my $stuff = read_file($file);
sub read_file {
my $file = shift;
open(FH,$file) or die "Can't open $file: $!\n";
my %times;
my ($name,$begin,$hashkey,$key,$day,$dom,$mon,$time,$hour);
while (<FH>) {
chomp;
if ($_ =~ /&N/) {
($name) = ($_ =~ /(\w+\s\w+|\w+\s\w+\s\w+|\w+\s\w+-\w+)$/);
}
if ($_ =~ /&D/) {
($begin) = ($_ =~ /(\w+\s+\d+-\w+-\d+)$/);
}
next if ($_ !~ /^(x|j|k|z)/);
# day dom mon
time hour
($key,$day,$dom,$mon,$time,$hour) = ($_ =~ /^(\w)\s+(\w+)\s+(\d+)-(\w+)-
\d+\s+(\d+:\d+):.*(\d+:\d+|-\d+:-\d+)/);
$hashkey = $day.'-'.$dom;
$times{$hashkey}->{name} = $name;
$times{$hashkey}->{began} = $begin;
if ($key =~ /x/i ) {
$times{$hashkey} = {
name => $name,
began => $begin,
day => $day,
dom => $dom,
morning => $time,
};
}
}
elsif ($key =~ /z/) {
my ($h,$total) = ($_ =~ /\s+(\d+:\d+)\s+.*\s+(\d+:\d+)\s\$/);
$times{$hashkey} = {
home => $time,
total => $total,
};
}
}
print STDERR Dumper(%times);
return %times;
}
##########################################
########## Sample data ##################
&N: Joe Bloggs
&D: Tue 06-Jun-2006
%%
x Tue 06-Jun-2006 08:18:22 2006 2:11 [OKAY] $
j Tue 06-Jun-2006 12:51:33 2006 4:33 [OKAY] $
k Tue 06-Jun-2006 13:21:27 2006 0:30 OK+SHL $
z Tue 06-Jun-2006 16:18:52 2006 2:57 [OKAY] 7:20 $
~
%%
x Wed 07-Jun-2006 08:39:05 2006 0:44 [OKAY] $
j Wed 07-Jun-2006 13:11:23 2006 4:32 [OKAY] $
k Wed 07-Jun-2006 13:41:04 2006 0:30 OK+SHL $
z Wed 07-Jun-2006 17:51:18 2006 4:10 [OKAY] 8:42 $
~
%%
x Thu 08-Jun-2006 08:13:54 2006 2:06 [OKAY] $
j Thu 08-Jun-2006 13:57:55 2006 5:44 [OKAY] $
k Thu 08-Jun-2006 16:08:42 2006 2:10 OK+SHL $
z Thu 08-Jun-2006 16:09:03 2006 0:00 [OKAY] 5:44 $
~
%%
x Fri 09-Jun-2006 09:24:05 2006 1:05 [OKAY] $
j Fri 09-Jun-2006 13:10:56 2006 3:46 [OKAY] $
k Fri 09-Jun-2006 14:17:40 2006 1:06 OK+SHL $
z Fri 09-Jun-2006 18:37:38 2006 4:19 AFTER! 8:05 $
~
%%
##########################################
Dermot Paikkos
Network Administrator @ Science Photo Library
Phone: 0207 432 1100
Fax: 0207 286 8668
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>