D
Davidcollins001
I am looking to parse data into a complex structure of hashes and
arrays. I have been reading the data structures cookbook on the CPAN
website
(http://search.cpan.org/~nwclark/perl-5.8.7/pod/perldsc.pod#MORE_ELABORATE_RECORDS).
At the bottom they say that it is sometimes easier to have the file as
the data structure that would be used to create the structure in perl.
I was wondering how to do this? I have been playing around trying to
learn how to use complex structures, but I can't figure out how to get
perl to parse code into a hash? I did initially think of placing the
data structure in a file and setting the hash equal to it, but that
doesn't work. am I thinking too simplistic?
This is my data structure that I am playing around with in a separate
file:
may => [
[2,'train',20.00, 'expensive'],
[6,'train',15],
[19,'car',18,'best way']
],
june => [
[5,'car',2.00],
[9,'train',1],
[19,'cat',18,'best way']
],
july => [
[14,'plane',100],
[23,'boat',20]
],
And this is the code I have to read and print it out:
## read above from file into hash
open(DATA, "<$file");
my %HoLoL=<DATA>;
close(DATA);
## print each element separately
foreach my $date ( keys %HoLoL )
{
print "$date";
for(my $i=0; $i<($#{@{$HoLoL{$date}}}+1); $i++)
{
for(my $j=0; $j<($#{ $HoLoL{$date} [$i] }+1); $j++)
{
print "\t ${ $HoLoL{$date} [$i]}[$j] ";
}
print "\n";
}
}
print "\n";
(I know I can print it out easier and probably quicker with one less
loop at least, I just wanted to know how to get to each element.)
Thanks in advance
arrays. I have been reading the data structures cookbook on the CPAN
website
(http://search.cpan.org/~nwclark/perl-5.8.7/pod/perldsc.pod#MORE_ELABORATE_RECORDS).
At the bottom they say that it is sometimes easier to have the file as
the data structure that would be used to create the structure in perl.
I was wondering how to do this? I have been playing around trying to
learn how to use complex structures, but I can't figure out how to get
perl to parse code into a hash? I did initially think of placing the
data structure in a file and setting the hash equal to it, but that
doesn't work. am I thinking too simplistic?
This is my data structure that I am playing around with in a separate
file:
may => [
[2,'train',20.00, 'expensive'],
[6,'train',15],
[19,'car',18,'best way']
],
june => [
[5,'car',2.00],
[9,'train',1],
[19,'cat',18,'best way']
],
july => [
[14,'plane',100],
[23,'boat',20]
],
And this is the code I have to read and print it out:
## read above from file into hash
open(DATA, "<$file");
my %HoLoL=<DATA>;
close(DATA);
## print each element separately
foreach my $date ( keys %HoLoL )
{
print "$date";
for(my $i=0; $i<($#{@{$HoLoL{$date}}}+1); $i++)
{
for(my $j=0; $j<($#{ $HoLoL{$date} [$i] }+1); $j++)
{
print "\t ${ $HoLoL{$date} [$i]}[$j] ";
}
print "\n";
}
}
print "\n";
(I know I can print it out easier and probably quicker with one less
loop at least, I just wanted to know how to get to each element.)
Thanks in advance