Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order an array by number 2

Status
Not open for further replies.

AnnoyingTeen

Programmer
Joined
Aug 22, 2006
Messages
10
Location
US
I'm writing a program for a civic association's website. I've already made a news update page, but I'm stuck now on the events page. All I need this page to do is list the upcoming events, but I need the events to be in order.
It comes from a form with a lot of drop down boxes, I'll give you the script for the form, and for the processor, all I need now is to make it show (in date order) on the page.
Here's the form:
#!C:\Perl\bin\perl

print "Content-type:text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>New news</title>\n";
print "<!--\n";
print "<BODY>\n";
print "-->\n";
print "</head>\n";
print "<body>\n";
print "<form method=\"post\" action=\"UpEventRead.pl\">\n";
print "Event Type:";
print "<SELECT name=\"type\">\n";
print "<OPTION value=\"cshca\">CSHCA Event\n";
print "<OPTION value=\"other\">Other Event\n";
print "</SELECT><BR>\n";
print "Date:\n";
print "<SELECT Name=\"month\"><OPTION value=\"0\">January<OPTION value=\"1\">February<OPTION value=\"2\">March<OPTION value=\"3\">April<OPTION value=\"4\">May<OPTION value=\"5\">June<OPTION value=\"6\">July<OPTION value=\"7\">August<OPTION value=\"8\">September<OPTION value=\"9\">October<OPTION value=\"10\">November<OPTION value=\"11\">December</SELECT>\n";
print "<SELECT name=\"day\"><OPTION value=\"1\">1<OPTION value=\"2\">2<OPTION value=\"3\">3<OPTION value=\"4\">4<OPTION value=\"5\">5<OPTION value=\"6\">6<OPTION value=\"7\">7<OPTION value=\"8\">8<OPTION value=\"9\">9<OPTION value=\"10\">10<OPTION value=\"11\">11<OPTION value=\"12\">12<OPTION value=\"13\">13<OPTION value=\"14\">14<OPTION value=\"15\">15<OPTION value=\"16\">16<OPTION value=\"17\">17<OPTION value=\"18\">18<OPTION value=\"19\">19<OPTION value=\"20\">20<OPTION value=\"21\">21<OPTION value=\"22\">22<OPTION value=\"23\">23<OPTION value=\"24\">24<OPTION value=\"25\">25<OPTION value=\"26\">26<OPTION value=\"27\">27<OPTION value=\"28\">28<OPTION value=\"29\">29<OPTION value=\"30\">30<OPTION value=\"31\">31</SELECT>\n";
print ", <SELECT Name=\"year\"><OPTION value=\"2006\">2006<OPTION value=\"2007\">2007<OPTION value=\"2008\">2008<OPTION value=\"2009\">2009<OPTION value=\"2010\">2010</select>\n";
print "<BR>\n";
print "Event: <textarea name=\"event\" cols=\"50\" rows=\"10\"></textarea><br>\n";
print "Password: <input type=\"password\" name=\"pass\"></input><br>\n";
print "<input name=\"button\" type=\"submit\" value=\"Submit\">\n";
print "</FORM>\n";
print "<!--\n";
print "</BODY>\n";
print "-->\n";
print "</body>\n";
print "</html>\n";

And here's the script that puts the data into a file:
#!C:\Perl\bin\perl

print "Content-type:text/html\n\n";

$pass = 0;
if( $ENV{'REQUEST_METHOD'} eq 'POST')
{read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'})};
@pairs = split(/&/, $buffer);
print "<html><head>\n";
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";
print "<META HTTP-EQUIV=Refresh CONTENT=\"10; URL=\"Newnews.pl\">\n";
($key, $password) = split(/=/, $pairs[5]);
if($password eq "password")
{
foreach $pair( @pairs )
{
($key, $value) = split( /=/, $pair);
$value =~ tr/+/ /;
$key =~ s/%(..)/pack("c", hex($1))/eg;
$key =~ tr/+/ /;
$value =~ s/%(..)/pack("c", hex($1))/eg;
$value =~ s/fuck/****/;
$value =~ s/shit/****/;
$value =~ s/damn/****/;
$value =~ s/<!--(.|\n)*-->//g;
if( $formdata{$key} ){formdata{$key} .= ", $value"; }
else{ $formdata{$key} = $value; }
}
if($formdata{type} eq 'cshca') { open(Event,">>cshcaevent.txt"); }
else { open(Event, ">>cshcaotherevent.txt"); }
print Event "$formdata{'month'}&$formdata{'day'}&$formdata{'year'}&$formdata{'event'}\n";
close(Event);
$pass = 1;
print "<HEAD>\n";
print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
print "<!-- Hide from older browsers\n";
print "alert('Your change has been made');\n";
print "// end hiding -->\n";
print "</SCRIPT>\n";
print "</HEAD>\n";
}
else {
print "<HEAD>\n";
print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
print "<!-- Hide from older browsers\n";
print "alert('Incorrect Password'}');\n";
print "// end hiding -->\n";
print "</SCRIPT>\n";
print "</HEAD>\n";
}
print "</head><body background=\"print "<CENTER><TABLE height=\"100\%\"><TR><TD valign=\"middle\"><TABLE valign=\"center\" \"\#000000\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\"><TR><TD bgcolor=\"\#AAAAFF\"><CENTER><BR><BR>Please wait while we forward you back to the news update screen.<BR><BR></TD></TR><TR><TD bgcolor=\"\#0000FF\"><A HREF=\"Newevent.pl\"><CENTER>Or click here if you do not wish to wait.</A></TD></TR></TABLE></TD></TR></TABLE></CENTER>\n";
print "<!-- </body> --></body></html>\n";
print "</HTML>\n";

I dont think it should be that hard to make this, the data for the news page, to put it in order (I've already started to make changes):

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<head>\n";
print "<title></title>\n";
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";
print "</HEAD>\n";
print "<body background=\"print "<BR><HR><BR><H1>CSHCA Events</H1></CENTER>\n";
open(events, "<cshcaevent.txt");
@events = <psts>;
close (psts);


foreach $line (@posts)
{
$post =~ s/fuck/****/;
$post =~ s/shit/****/;
$post =~ s/damn/****/;
print "<TABLE border=\"0\" width=\"100%\">\n";
print "<TR><TD bgcolor=\"666600\"><FONT color=\"FFFFFF\">Post By $name <div align=\"right\">$time</div></TD></TR>\n";
print "<TR><TD>$post</TD></TR>\n";
print "</TABLE>\n";
print "<BR>\n";
}

print "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100\%\">\n";
print "<tr>\n";
print "<td bgcolor=\"\#666600\"><font color=\"\#ffffff\">Post by Richard Balsano<div align=\"right\">12:02:00 Sunday, September 25</td></tr>\n";
print " <TR><TD><P>The SHUFSD voted to cancel their contract to buy Mohlenhoff property. We are on the Task Force to find an alternate bus depot site. Phase 2 now starts -- getting the town and county to buy the land for use in an environment and community friendly way.</P></TD></TR>\n";
print "</table>\n";
print "<CENTER><BR><HR><BR><H1>Site News</H1></CENTER>\n";
print "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100\%\">\n";
print "<tr>\n";
print "<td bgcolor=\"\#666600\"><font color=\"\#ffffff\">Post by Jonathan Balsano<div align=\"right\">07:21:00 Sunday, August 7</td></tr>\n";
print " <TR><TD><P>The website is ready, although we don't have many links yet...I hope this site will really help the CSHCA with communications between the members and getting their message out.</P></TD></TR>\n";
print "</table>\n";
print "<!--\n";
print "</Body>\n";
print "-->\n";
print "</Body>\n";
print "</html>\n";

So if you haven't figured out what I'm trying to do yet, because I'm really horrible at explaining my problems, I need to know the most effective way to arrange the events in date order when displayed
 
to sort things you need to use the SORT command...
I have an array of hashes @tune and want to sort by the 'key' [votes] so use this...
Code:
# Max Votes
my @maxvotes = sort ({$b->{'votes'} <=> $a->{'votes'}} @tune);


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Okay, now I know the command, but its being sorted in multiple ways, first by year, then month, then day. Each is contained in the array, so how can I rearrange the array so as it goes through to post it all, its in the correct order? (I'll keep looking at it)
 
I always use epoch seconds which is a direct reference to the actual date and time and is a single number.

Keith
 
Never heard of em, could you elaborate?
(I'm 14 so I don't know too much, I'm trying to gain experience by doing this for my area's civic association)
 
(Sorry for the double post couldnt find an edit button)
Ive looked into epoch time and I dont really understand how to convert regular time to epoch time and back again, but I do know what it is, could someone help me convert it?
 
How is the news stored on the server?
 
Here is what I now have:
Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<head>\n";
print "<title></title>\n";
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";
print "</HEAD>\n";
print "<body background=\"[URL unfurl="true"]http://fire.prohosting.com/jabal/CSHCA/background.GIF\">\n";[/URL]
print "<BR><CENTER><BR><H1>CSHCA Events</H1></CENTER>\n";
open(events, "<cshcaevent.txt");
@events = <events>;
close (events);
foreach $line (@events)
{
    $line =~ s/fuck/****/;
    $line =~ s/shit/****/;
    $line =~ s/damn/****/;
}
foreach $event (@events) {
($Month, $day, $year, $evnt) = split( /&/, $event);
use Switch;
if ($Month eq "0") {$Month = "January"}
if($Month eq "1") {$Month = "February"}
if($Month eq "2") {$Month = "March"}
if($Month eq "3") {$Month = "April"}
if($Month eq "4") {$Month = "May"}
if($Month eq "5") {$Month = "June"}
if($Month eq "6") {$Month = "July"}
if($Month eq "7") {$Month = "August"}
if($Month eq "8") {$Month = "September"}
if($Month eq "9") {$Month = "October"}
if($Month eq "10") {$Month = "November"}
if($Month eq "11") {$Month = "December"}
print "$Month $day, $year - $evnt <BR>";
}
print "<HR><CENTER><H1>Other Events</H1></CENTER><BR>";
open(events, "<cshcaotherevent.txt");
@events = <events>;
close (events);
foreach $line (@events)
{
    $line =~ s/fuck/****/;
    $line =~ s/shit/****/;
    $line =~ s/damn/****/;
}
foreach $event (@events) {
($Month, $day, $year, $evnt) = split( /&/, $event);
use Switch;
if ($Month eq "0") {$Month = "January"}
if($Month eq "1") {$Month = "February"}
if($Month eq "2") {$Month = "March"}
if($Month eq "3") {$Month = "April"}
if($Month eq "4") {$Month = "May"}
if($Month eq "5") {$Month = "June"}
if($Month eq "6") {$Month = "July"}
if($Month eq "7") {$Month = "August"}
if($Month eq "8") {$Month = "September"}
if($Month eq "9") {$Month = "October"}
if($Month eq "10") {$Month = "November"}
if($Month eq "11") {$Month = "December"}
print "$Month $day, $year - $evnt <BR>";
}
print "<!--\n";
print "</Body>\n";
print "-->\n";
print "</Body>\n";
print "</html>\n";
[\CODE]

What I had posted before was me in the process of modifying the news, sorry about that. But the data is stored in a text file in the order of (Month)&(Day)&(Year)&(Event). Im using the & as a delimeter...if thats the right word.
 
I rewrote the code you posted to hopefully make it work easily. Replace the script you posted above with this code:

Code:
#!/usr/bin/perl
%Months = (
   0 => 'January',
   1 => 'February',
   2 => 'March',
   3 => 'April',
   4 => 'May',
   5 => 'June',
   6 => 'July',
   7 => 'August',
   8 => 'September',
   9 => 'October',
   10 => 'November',
   11 => 'December',
);

print qq~Content-type: text/html\n\n
<head>
<title></title>
</head>
<body background="[URL unfurl="true"]http://fire.prohosting.com/jabal/CSHCA/background.GIF">[/URL]
<BR><CENTER><BR><H1>CSHCA Events</H1></CENTER>
~;	

open(EVENTS, "<cshcaevent.txt") or die "$!";
@events = <EVENTS>;
close(EVENTS);

sort_and_print(@events);

print "<HR><CENTER><H1>Other Events</H1></CENTER><BR>";

open(EVENTS, "<cshcaotherevent.txt") or die "$!";
@events = <EVENTS>;
close(EVENTS);

sort_and_print(@events);

print qq~</Body></html>~;

sub sort_and_print {
   my @events = @_;
   @events = map{"$Months{$_->[0]} $_->[1], $_->[2] - $_->[3]<br>"}
             sort{$b->[2]<=>$a->[2] ||
                  $b->[1]<=>$a->[1] ||
                  $b->[0]<=>$a->[0]}
             map{chomp;[split/&/]} @events;
   foreach my $event (@events) {
      $event =~ s/fuck/****/gi;
      $event =~ s/shit/****/gi;
      $event =~ s/damn/****/gi;
      print $event;
  }
}

to sort and print each file is now a sub routine that can be shared by both files since the output is the same for both. The months are now a hash instead of the un-perlish switch statments you were using. The sorting is accomplish by using whats called a Schwartzian Transform. The printing is cleaned up, but I am unsure what this block of html code was for so left it out:

Code:
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";

it really seems to make no sense and surely is bad html code anyway.
 
Okay, I use a free service, so that block of code was to get rid of their ads. :P

And if you get a chance, could you explain to me how what you changed works? I found that it doesnt print the month too. I'm a little slow at this stuff so I dont think I understand how anything you changed works. >.>
 
you posted this:

12:02:00 Sunday, September 25 (followed by the text of event)

but later you have:

Code:
($Month, $day, $year, $evnt) = split( /&/, $event);
use Switch;
if ($Month eq "0") {$Month = "January"}

So I don't see how the time/date you posted is getting extracted from the file. $Month looks like it should be the time: 12:02:00

post a real sample of the data you are using in the files to display the events.
 
Okay. here
This is cshcaevents.pl it displays the events.
Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<head>\n";
print "<title></title>\n";
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";
print "</HEAD>\n";
print "<body background=\"[URL unfurl="true"]http://fire.prohosting.com/jabal/CSHCA/background.GIF\">\n";[/URL]
print "<BR><CENTER><BR><H1>CSHCA Events</H1></CENTER>\n";
open(events, "<cshcaevent.txt");
@events = <events>;
close (events);
foreach $line (@events)
{
    $line =~ s/fuck/****/;
    $line =~ s/shit/****/;
    $line =~ s/damn/****/;
}
foreach $event (@events) {
($Month, $day, $year, $evnt) = split( /&/, $event);
use Switch;
if ($Month eq "0") {$Month = "January"}
if($Month eq "1") {$Month = "February"}
if($Month eq "2") {$Month = "March"}
if($Month eq "3") {$Month = "April"}
if($Month eq "4") {$Month = "May"}
if($Month eq "5") {$Month = "June"}
if($Month eq "6") {$Month = "July"}
if($Month eq "7") {$Month = "August"}
if($Month eq "8") {$Month = "September"}
if($Month eq "9") {$Month = "October"}
if($Month eq "10") {$Month = "November"}
if($Month eq "11") {$Month = "December"}
print "$Month $day, $year - $evnt <BR>";
}
print "<HR><CENTER><H1>Other Events</H1></CENTER><BR>";
open(events, "<cshcaotherevent.txt");
@events = <events>;
close (events);
foreach $line (@events)
{
    $line =~ s/fuck/****/;
    $line =~ s/shit/****/;
    $line =~ s/damn/****/;
}
foreach $event (@events) {
($Month, $day, $year, $evnt) = split( /&/, $event);
use Switch;
if ($Month eq "0") {$Month = "January"}
if($Month eq "1") {$Month = "February"}
if($Month eq "2") {$Month = "March"}
if($Month eq "3") {$Month = "April"}
if($Month eq "4") {$Month = "May"}
if($Month eq "5") {$Month = "June"}
if($Month eq "6") {$Month = "July"}
if($Month eq "7") {$Month = "August"}
if($Month eq "8") {$Month = "September"}
if($Month eq "9") {$Month = "October"}
if($Month eq "10") {$Month = "November"}
if($Month eq "11") {$Month = "December"}
print "$Month $day, $year - $evnt <BR>";
}
print "<!--\n";
print "</Body>\n";
print "-->\n";
print "</Body>\n";
print "</html>\n";

This is Newevent.pl It creates the form for the data:
Code:
#!/usr/bin/perl

print "Content-type:text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>New news</title>\n";
print "<!--\n";
print "<BODY>\n";
print "-->\n";
print "</head>\n";
print "<body>\n";
print "<form method=\"post\" action=\"UpEventRead.pl\">\n";
print "Event Type:";
print "<SELECT name=\"type\">\n";
print "<OPTION value=\"cshca\">CSHCA Event\n";
print "<OPTION value=\"other\">Other Event\n";
print "</SELECT><BR>\n";
print "Date:\n";
print "<SELECT Name=\"month\"><OPTION value=\"0\">January<OPTION value=\"1\">February<OPTION value=\"2\">March<OPTION value=\"3\">April<OPTION value=\"4\">May<OPTION value=\"5\">June<OPTION value=\"6\">July<OPTION value=\"7\">August<OPTION value=\"8\">September<OPTION value=\"9\">October<OPTION value=\"10\">November<OPTION value=\"11\">December</SELECT>\n";
print "<SELECT name=\"day\"><OPTION value=\"1\">1<OPTION value=\"2\">2<OPTION value=\"3\">3<OPTION value=\"4\">4<OPTION value=\"5\">5<OPTION value=\"6\">6<OPTION value=\"7\">7<OPTION value=\"8\">8<OPTION value=\"9\">9<OPTION value=\"10\">10<OPTION value=\"11\">11<OPTION value=\"12\">12<OPTION value=\"13\">13<OPTION value=\"14\">14<OPTION value=\"15\">15<OPTION value=\"16\">16<OPTION value=\"17\">17<OPTION value=\"18\">18<OPTION value=\"19\">19<OPTION value=\"20\">20<OPTION value=\"21\">21<OPTION value=\"22\">22<OPTION value=\"23\">23<OPTION value=\"24\">24<OPTION value=\"25\">25<OPTION value=\"26\">26<OPTION value=\"27\">27<OPTION value=\"28\">28<OPTION value=\"29\">29<OPTION value=\"30\">30<OPTION value=\"31\">31</SELECT>\n";
print ", <SELECT Name=\"year\"><OPTION value=\"2006\">2006<OPTION value=\"2007\">2007<OPTION value=\"2008\">2008<OPTION value=\"2009\">2009<OPTION value=\"2010\">2010</select>\n";
print "<BR>\n";
print "Event: <textarea name=\"event\" cols=\"50\" rows=\"10\"></textarea><br>\n";
print "Password: <input type=\"password\" name=\"pass\"></input><br>\n";
print "<input name=\"button\" type=\"submit\" value=\"Submit\">\n";
print "</FORM>\n";
print "<!--\n";
print "</BODY>\n";
print "-->\n";
print "</body>\n";
print "</html>\n";

And this is UpEventRead.pl It takes the data from the form and puts it in text files.

Code:
#!/usr/bin/perl

print "Content-type:text/html\n\n";

$pass = 0;
if( $ENV{'REQUEST_METHOD'} eq 'POST')
{read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'})};
@pairs = split(/&/, $buffer);
print "<html><head>\n";
print "<div name=\"Ad\" style=\"display:none\">\n";
print "<textarea><body></textarea> \n";
print "</div>\n";
print "<META HTTP-EQUIV=Refresh CONTENT=\"10; URL=\"Newnews.pl\">\n";
($key, $password) = split(/=/, $pairs[5]);
if($password eq "enviro19")
{
 foreach $pair( @pairs )
  {
   ($key, $value) = split( /=/, $pair);
   $value =~ tr/+/ /;
   $key =~ s/%(..)/pack("c", hex($1))/eg;
   $key =~ tr/+/ /;
   $value =~ s/%(..)/pack("c", hex($1))/eg;
   $value =~ s/fuck/****/;
   $value =~ s/shit/****/;
   $value =~ s/damn/****/;
   $value =~ s/<!--(.|\n)*-->//g;
   if( $formdata{$key} ){formdata{$key} .= ", $value"; }
   else{ $formdata{$key} = $value; }
  }
  if($formdata{type} eq 'cshca') { open(Event,">>cshcaevent.txt"); }
  else { open(Event, ">>cshcaotherevent.txt"); }
  print Event "$formdata{'month'}&$formdata{'day'}&$formdata{'year'}&$formdata{'event'}\n";
  close(Event);
  $pass = 1;
    print "<HEAD>\n";
    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
    print "<!-- Hide from older browsers\n";
    print "alert('Your change has been made');\n";
    print "// end hiding -->\n";
    print "</SCRIPT>\n";
    print "</HEAD>\n";
}
else {
    print "<HEAD>\n";
    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
    print "<!-- Hide from older browsers\n";
    print "alert('Incorrect Password'}');\n";
    print "// end hiding -->\n";
    print "</SCRIPT>\n";
    print "</HEAD>\n";
     }
print "</head><body background=\"[URL unfurl="true"]http://fire.prohosting.com/jabal/CSHCA/background.GIF\">\n";[/URL]
print "<CENTER><TABLE height=\"100\%\"><TR><TD valign=\"middle\"><TABLE valign=\"center\" \"\#000000\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\"><TR><TD bgcolor=\"\#AAAAFF\"><CENTER><BR><BR>Please wait while we forward you back to the news update screen.<BR><BR></TD></TR><TR><TD bgcolor=\"\#0000FF\"><A HREF=\"Newevent.pl\"><CENTER>Or click here if you do not wish to wait.</A></TD></TR></TABLE></TD></TR></TABLE></CENTER>\n";
print "<!-- </body> --></body></html>\n";
print "</HTML>\n";

And here is an example of an event post put in a code box for readability(yes those are random words I used for testing.):
Code:
January&1&2006&cheesey event
January&1&2006&event
January&1&2006&roogoo

Okay, after going through the process of copying and pasting everything I found that the text files all say the months, so I did some testing, changed them to 0's, rather than the verbal form and your script worked. But going back I cant figure out why its printing to the text file with the month, as well as where the month is coming from. A little help?
 
Okay, that was really bad, everything works. I forgot to save changes -.-

I'll implement everything right now, but could you please tell me how your script works?
 
if have posted the url of the script and the password so you need to change that password if it's the real one.
 
Code:
#!/usr/bin/perl
[b]# 1. A hash to convert the number of the month to the name of the month[/b]

%Months = (
   0 => 'January',
   1 => 'February',
   2 => 'March',
   3 => 'April',
   4 => 'May',
   5 => 'June',
   6 => 'July',
   7 => 'August',
   8 => 'September',
   9 => 'October',
   10 => 'November',
   11 => 'December',
);

[b]# 2. Just prints the html header stuff but uses the 'qq' operator instead of multiple print lines and double-quotes. When you use 'qq' it automatically escapes any double-quotes in the string so you don't have to do this: \". The ~ is just an arbitrary delimiter to tell perl where qq begins and ends.[/b]

print qq~Content-type: text/html\n\n
<head>
<title></title>
</head>
<body background="[URL unfurl="true"]http://fire.prohosting.com/jabal/CSHCA/background.GIF">[/URL]
<BR><CENTER><BR><H1>CSHCA Events</H1></CENTER>
~;    

[b]# 3. Opens the file for reading but kills the script (die) and sends an error message to STDOUT if the file can't be opened.[/b]

open(EVENTS, "<cshcaevent.txt") or die "$!";
@events = <EVENTS>;
close(EVENTS);

[b]# 4. Calls the sub routine to sort the data and print it. Passes the data (the array) to the sub in the system array, the parenthesis after the sub routine name: (@events)[/b]

sort_and_print(@events);

print "<HR><CENTER><H1>Other Events</H1></CENTER><BR>";

[b]# Same as 3[/b]

open(EVENTS, "<cshcaotherevent.txt") or die "$!";
@events = <EVENTS>;
close(EVENTS);

[b]# Same as 4[/b]
sort_and_print(@events);

[b]# 5. Prinst end of html code[/b]
print qq~</Body></html>~;

[b]# 6. The sub routine to sort and print the data[/b]
sub sort_and_print {
   [b]# Get the data back out of the system array.[/b]
   my @events = @_;
   [b]# Sort the data using a complex bit of code and format the string for print[/b] 
   @events = map{"[b]$Months{$_->[0]}[/b] $_->[1], $_->[2] - $_->[3]<br>"}
             sort{$b->[2]<=>$a->[2] ||
                  $b->[1]<=>$a->[1] ||
                  $b->[0]<=>$a->[0]}
             map{chomp;[split/&/]} @events;
   [b]# Remove vulgar words and print the data[/b]
   foreach my $event (@events) {
      $event =~ s/fuck/****/gi;
      $event =~ s/shit/****/gi;
      $event =~ s/damn/****/gi;
      print $event;
  }
}

this is where the number of the month gets converted to the name: $Months{$_->[0]}

this bit of cryptic looking code is called a Schwartzian Transform:

Code:
   @events = map{"[b]$Months{$_->[0]}[/b] $_->[1], $_->[2] - $_->[3]<br>"}
             sort{$b->[2]<=>$a->[2] ||
                  $b->[1]<=>$a->[1] ||
                  $b->[0]<=>$a->[0]}
             map{chomp;[split/&/]} @events;

There is some reading material about it here:


but it really does not explain well what the code is doing. You have to start learning references and anonymous data storage to understand it.
 
Epoch seconds is a number representing the number of seconds which have elapsed since January 1st. 1970.
It might seem a little odd but it really is a very useful number. If you use localtime in the standard format
Code:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
the var 'time' is the epoch value of the time now, and the localtime function breaks it down into seperate components.
The variable 'time' may be replaced, in the function above, with any epoch value and the component parts represent the decoded time and date.
The real beauty of it is that the values are very easy to order as they are single numbers.

Keith
 
Okay, thank you everyone, I understand how most of that works up to the Schwartzian transform, and I understand how to get the regular time out of epoch time, but not epoch seconds out of regular human time. If I could find an easy way to do it without a REALLY long module, I think it would be an easier way and wouldnt require the schwartzian transform. So that information would be helpful, but I'll use KEvin's code for now. Thanks everyone.
 
I use the following call to the sub routine.
It works but can anyone tell me why, without the red highlighted part, the day is +1?

Code:
my $epo=&Make_Epoc_Value($DAY,$MONTH,$YEAR);
Code:
sub Make_Epoc_Value{
	my @MUNFS=(0,31,28,31,30,31,30,31,31,30,31,31,30,31,30,31);
	# $_[0] - DAYS
	# $_[1] - MONTH
	# $_[2] - YEAR
	my $Sekunds;
	my $x;
	# !!!!! Calculate the number of seconds for the years
	for($x=1970; $x<$_[2]; ++$x){
		$Sekunds+=31536000; # Seconds in Year

		my $LEAP=$x % 4;
		if($LEAP == 0){
			$Sekunds+=86400;
		}
	}

	# !!!!! Calculate the number of seconds for the Whole Months

	for($x=1; $x<$_[1]; ++$x){
		$Sekunds+=$MUNFS[$x]*86400;
	}
	$Sekunds+=$_[0]*86400;

[red]
	# !!!!! If year is non leap year day is 1 too many so take one off


	if(($_[2] % 4) != 0){
		$Sekunds-=86400
	}	
[/red]
	return($Sekunds);
}

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top