P
Paul E. Schoen
I am trying to construct an array (or possibly a hash) which consists of
arrays. It is organized as a series of records with four text fields:
Date, Time, Title, Description
I was able to get this to work with the following ugly but functional code:
my @E_Dates = ("20100831", "20100910");
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Example";
my $E_Descr = "Line 1 \nLine2";
my $Event_DB = [[$E_Dates[0], $E_Time, $E_Title, $E_Descr]];
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Example Title 2";
$E_Descr = "Example 2 Line 1 \nExample 2 Line2";
@E_Details = ($E_Time, $E_Title, $E_Descr); # Array of three elements
$Event_DB->[1] =[ $E_Dates[1], $E_Time, $E_Title, $E_Descr];
print "$Event_DB->[0][0]"; #prints first Date
print "$Event_DB->[0][1]"; #prints first Time
print "$Event_DB->[0][2]"; #prints first Title
print "$Event_DB->[0][3]"; #prints first Descr
print "$Event_DB->[1][0]"; #prints second Date
print "$Event_DB->[1][1]"; #prints second Time
print "$Event_DB->[1][2]"; #prints second Title
print "$Event_DB->[1][3]"; #prints second Descr
But when I tried to use an array, I could not use the two dimensional
indexes:
my @E_Dates = ("20100831", "20100910"); #Date in yyyymmdd format
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Title1";
my $E_Descr = "Descr1-Line1 \nDescr1-Line2";
my @Event_DB = ($E_Dates[0], $E_Time, $E_Title, $E_Descr);
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Title2";
$E_Descr = "Descr2-Line1 \nDescr2-Line2";
push( @{Event_DB}, $E_Dates[1], $E_Time, $E_Title, $E_Descr );
for (my $i=0; $i<@Event_DB; $i+=4) {
print "<p>";
print "<h3>Title $i: $Event_DB[$i+2]</h3>\n";
print "<br><h4>Date: $Event_DB[$i]</h4>\n";
print "<br><h4>Time: $Event_DB[$i+1]</h4>\n";
print "<br><h5>Description: $Event_DB[$i+3]</h5></p><hr>\n";
}
I tried everything I could find and I had no success. I originally tried to
create a hash with the date as the index and an array for the data, about
like this:
my @E_Dates = ("20100831", "20100910");
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Example";
my $E_Descr = "Line 1 \nLine2";
# Hash with Date as sorting key, Array of Details
my %Event_DB = (Date=>$E_Dates[0], Details=>[$E_Time, $E_Title,
$E_Descr]);
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Example Title 2";
$E_Descr = "Example 2 Line 1 \nExample 2 Line2";
@E_Details = ($E_Time, $E_Title, $E_Descr); # Array of three elements
# Adding second record with push
push @{ $Event_DB{Date=>$E_Dates[1]}}, Details=>[$E_Time, $E_Title,
$E_Descr];
# Alternate method (probably very wrong)
%Event_DB(Date=>$E_Dates[1]) = Details=>[$E_Time, $E_Title, $E_Descr];
I would like to be able to sort the array or hash by date and then format it
to be printed as HTML. I am more familiar with C and Borland Delphi Pascal
where this would be simple. It's probably also simple in Perl but I can't
figure out how to do it.
TIA,
Paul
arrays. It is organized as a series of records with four text fields:
Date, Time, Title, Description
I was able to get this to work with the following ugly but functional code:
my @E_Dates = ("20100831", "20100910");
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Example";
my $E_Descr = "Line 1 \nLine2";
my $Event_DB = [[$E_Dates[0], $E_Time, $E_Title, $E_Descr]];
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Example Title 2";
$E_Descr = "Example 2 Line 1 \nExample 2 Line2";
@E_Details = ($E_Time, $E_Title, $E_Descr); # Array of three elements
$Event_DB->[1] =[ $E_Dates[1], $E_Time, $E_Title, $E_Descr];
print "$Event_DB->[0][0]"; #prints first Date
print "$Event_DB->[0][1]"; #prints first Time
print "$Event_DB->[0][2]"; #prints first Title
print "$Event_DB->[0][3]"; #prints first Descr
print "$Event_DB->[1][0]"; #prints second Date
print "$Event_DB->[1][1]"; #prints second Time
print "$Event_DB->[1][2]"; #prints second Title
print "$Event_DB->[1][3]"; #prints second Descr
But when I tried to use an array, I could not use the two dimensional
indexes:
my @E_Dates = ("20100831", "20100910"); #Date in yyyymmdd format
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Title1";
my $E_Descr = "Descr1-Line1 \nDescr1-Line2";
my @Event_DB = ($E_Dates[0], $E_Time, $E_Title, $E_Descr);
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Title2";
$E_Descr = "Descr2-Line1 \nDescr2-Line2";
push( @{Event_DB}, $E_Dates[1], $E_Time, $E_Title, $E_Descr );
for (my $i=0; $i<@Event_DB; $i+=4) {
print "<p>";
print "<h3>Title $i: $Event_DB[$i+2]</h3>\n";
print "<br><h4>Date: $Event_DB[$i]</h4>\n";
print "<br><h4>Time: $Event_DB[$i+1]</h4>\n";
print "<br><h5>Description: $Event_DB[$i+3]</h5></p><hr>\n";
}
I tried everything I could find and I had no success. I originally tried to
create a hash with the date as the index and an array for the data, about
like this:
my @E_Dates = ("20100831", "20100910");
my $E_Time = "14:00"; #Time in hh:mm format
my $E_Title = "Example";
my $E_Descr = "Line 1 \nLine2";
# Hash with Date as sorting key, Array of Details
my %Event_DB = (Date=>$E_Dates[0], Details=>[$E_Time, $E_Title,
$E_Descr]);
$E_Time = "18:00"; #Time in hh:mm format
$E_Title = "Example Title 2";
$E_Descr = "Example 2 Line 1 \nExample 2 Line2";
@E_Details = ($E_Time, $E_Title, $E_Descr); # Array of three elements
# Adding second record with push
push @{ $Event_DB{Date=>$E_Dates[1]}}, Details=>[$E_Time, $E_Title,
$E_Descr];
# Alternate method (probably very wrong)
%Event_DB(Date=>$E_Dates[1]) = Details=>[$E_Time, $E_Title, $E_Descr];
I would like to be able to sort the array or hash by date and then format it
to be printed as HTML. I am more familiar with C and Borland Delphi Pascal
where this would be simple. It's probably also simple in Perl but I can't
figure out how to do it.
TIA,
Paul