#!/usr/bin/perl -w # # Test code to read event database for Ted Wang # # S. Diggs: 2003.11.05 # require "fix_date.pl"; require "html_creation.pl"; ##print STDOUT "Enter database filename: "; ##chomp($input_filename = ); ##print STDOUT "Input filename is: $input_filename\n\n"; #$input_filename = "2004.02.27_event.hist"; my $input_filename = "Current_Event_Hist.txt"; if (! ( -e $input_filename)){ print "$input_filename cannot be found in the current directory!\n"; exit(1); } print "Reading database file...\n"; open (INPUT, $input_filename) or die "Cannot open $input_filename for reading"; # #--> leave in line feeds (the 'NOTE' section of the database is multi-line #--> only separated by the '$' character) # @file_buffer = ; print STDOUT "Got ", ($#file_buffer + 1), " lines\n"; close(INPUT); # #--> In the database file, the first line is field names, the last #--> line is a string of $'s to signal the end. #--> read in fields from 1st line #--> Database is held in an array with each line as a separate element # chomp($file_buffer[0]); my @event_field = split (/\$/, $file_buffer[0]); print STDOUT "Fields are: \n---\n"; $num_of_fields = 0; foreach my $thing (@event_field) { $num_of_fields++; print STDOUT $thing . "\n"; } print STDOUT "\nThere are $num_of_fields fields in the events table\n"; # #--> In the database file, the first line is field names, the last #--> line is a string of $'s to signal the end. #--> Keep the first line for completeness. Remove the last line. # pop(@file_buffer); # #--> Database is in the format data$data$data\ndata$data$data. #--> If it is parsed by $, the third element will be data\ndata. #--> Cannot parse by \n, since they are allowed in the NOTES #--> section. # #--> My workaround (not necessarily the best) is to change the format to #--> data$data$data\n$data$data$data while it is still in the file_buffer #--> array. First see if a line is a new entry. Lines that are new #--> entries have dollar signs. Lines within notes do not. If it is #--> a new entry, insert a dollar sign at the beginning. Do this for all #--> lines except for the first line in the file. # #--> Line by line, the original database looks like this: #--> line 1 (new entry): data$data$data$notes\n #--> line 2 (new entry): data$data$data$notes\n #--> line 3 (continuation): notes\n # #--> Line by line, the adjusted database looks like this: #--> line 1 (new entry): data$data$data$notes\n #--> line 2 (new entry): $data$data$data$notes\n #--> line 3 (continuation): notes\n # #--> NOTE: this will work as long as newline chars #--> are allowed only in the last field. # $num_of_entries = 0; ##open(OUTPUT_FILE, ">fixtest"); for ($i=1; $i <= $#file_buffer; $i++) { if ($file_buffer[$i] =~ /\$/) { $num_of_entries++; $file_buffer[$i] = "\$$file_buffer[$i]"; ## print OUTPUT_FILE "$file_buffer[$i]"; } } ##close(OUTPUT_FILE); # #--> There are $num_of_entries entries plus 1 line with the fields # print "There are $num_of_entries entries\n"; # #--> Join the file input buffer back together so that you #--> can parse the multi-line 'NOTES' section at the end #--> Database is now held in one long string # $input_buffer_strung_together = join('', @file_buffer); # #--> Re-parse the scalar "$input_buffer_strung_together" by "$" #--> Database is now held in an array, each element is text #--> between the dollar signs in the original file # @reparse_input_buffer = split(/\$/, $input_buffer_strung_together); # #--> Now, print out the array elements, one at a time until you're #--> satisfied that it worked # #my $dummy = ; #for ($i=0 ; $i <= $#reparse_input_buffer; $i++) { ##for ($i=0 ; $i <= 1000; $i++) { #for ($i=60021 ; $i <= 60121; $i++) { #for ($i=25000 ; $i <= 25100; $i++) { ## if ($i % 13 == 0) { ## printf STDOUT ("%5d\t --> %s\n",$i, $reparse_input_buffer[$i]); ## } ##} # #--> Make a "virtual" database with a hash of arrays. #--> |0 |1 |2 |3|4|...|num_of_entries| #--> |line | -> |line |a02 |a03 |3|4|...|num_of_entries| #--> |date | -> |date |10/27/03 |02/28/94 |3|4|...|num_of_entries| #--> . #--> . #--> . #--> |notes| -> |notes |fixed |deleted |3|4|...|num_of_entries| # for ($i=0 ; $i <= $num_of_entries ; $i++) { my @temp_array = splice(@reparse_input_buffer, 0, $num_of_fields); $field=0; foreach my $array_element ( @event_field ) { # #--> set hash_element[i] = the line it's found on (i) #--> and the name of the proper field column # # print "$i||$array_element||$temp_array[$field]\n"; $data_hash{$array_element}[$i] = $temp_array[$field]; $field++; } } print "\nData hash created (PRESS ENTER TO CONTINUE): "; my $dummy = ; # #--> Check to see if the database was parsed correctly, if not, there may be #--> misplaced entries. One sign of misplaced entries is newlines in #--> fields other than notes. This will result in a num_of_entries count to #--> not equal the actual number of entries as given by th 4D database. # # #--> Change the date formats from (M or MM)/(D or DD)/YY to YYYY/MM/DD using #--> fix_date.pl # #--> Change newlines in Notes section to
for html pages # for ($i=1 ; $i <= $num_of_entries ; $i++) { $data_hash{'Cruise Date'}[$i] = &fix_date($data_hash{'Cruise Date'}[$i]); $data_hash{'Date Entered'}[$i] = &fix_date($data_hash{'Date Entered'}[$i]); $data_hash{'Note'}[$i]=~ s/\n/
\n/g; } # #--> Allow user to enter cmd line arguments to check data_hash. #--> Usage: #--> -e 06MT22_5 (-e followed by expocode for all entries with that code) #--> -i 456 (-i followed by index number for entry with that index number) # if ( $#ARGV > 0) { if ( grep(/-e/, $ARGV[0])) { for ($i=0; $i<=$num_of_entries; $i++) { if (grep(/$ARGV[1]/, $data_hash{'ExpoCode'}[$i])){ foreach $array_element (@event_field) { print "$i: $array_element: $data_hash{$array_element}[$i]\n"; } } } } if (grep(/-i/, $ARGV[0]) && $ARGV[1] =~ /[0-9]+/ && $ARGV[1] <= $num_of_entries && $ARGV[1] >= 0) { print "Entry $ARGV[1]:\n---------------\n"; foreach $array_element (@event_field) { print "$array_element: $data_hash{$array_element}[$ARGV[1]]\n"; } } } # #--> Open the data file with the list of all the expos and directories. #--> Use this info to search for entries and create their HTML pages. #--> This data file is $ delimited with 1 entry per line. # #$list_of_expos="/home/whpo/sdiggs/HTML/cgi/PERL/MAKE_TABLES/list_of_expos"; $list_of_expos="list_of_expos"; open(LIST, $list_of_expos) or print STDOUT "Cannot open datafile ---> $list_of_expos\n"; @expo_buffer = ; chomp(@expo_buffer); close(LIST); print "Reading list of expos file...\n"; # #--> Make a hash of arrays for this file we just opened. #--> |0 |1 |2 | ... |n | #--> |line| -> |a01 |a02 |a02 | ... |s04 | #--> |dir | -> |dir1 |dir2 |dir2 | ... |dir9 | #--> |expo| -> |121 |122 |123 | ... |9999 | # @expo_header=( "Line", "Directory", "Expocode" ); for ($c=0; $c<=$#expo_buffer ; $c++) { @expo_tmp = split(/\$/,$expo_buffer[$c]); $expo_field=0; foreach $name (@expo_header) { # print "$c||$name||$expo_tmp[$expo_field]\n"; $expo_hash{$name}[$c] = $expo_tmp[$expo_field]; $expo_field++; } } # #--> Some adjacent entries have the same directory. Collapse these entries #--> into one entry. Separate the expocodes with "and". This will be useful #--> in creating the HTML pages. Do not remove the entries with the same #--> directory. Instead, add a field to the hash to indicate which #--> entries to use when creating the HTML pages. #--> |0 |1 |2 | ... |n | #--> |line| -> |a01 |a02 |a02 | ... |s04 | #--> |dir | -> |dir1 |dir2 |dir2 | ... |dir9 | #--> |expo| -> |121 |122 |122 and 123 | ... |9999 | #--> |use | -> |true |false |true | ... |true | $expo_hash{'Use'}[0] = "true"; for ($i=1 ; $i <= $#expo_buffer ; $i++ ) { $expo_hash{'Use'}[$i] = "true"; if(grep(/$expo_hash{'Directory'}[$i]/, $expo_hash{'Directory'}[$i-1])){ $expo_hash{'Expocode'}[$i] = "$expo_hash{'Expocode'}[$i-1] and $expo_hash{'Expocode'}[$i]"; $expo_hash{'Use'}[$i-1] = "false"; } } # count how many directories are used #$numfiles = 0; #for ($i=0; $i<= $#expo_buffer; $i++) { # if (grep(/$expo_hash{'Use'}[$i]/, "true")) # {$numfiles++;} #} #print $numfiles; print "Expo hash created (PRESS ENTER TO CONTINUE): "; my $dummy2 = ; # #--> For each directory, use its expocodes to search for the corresponding #--> expocodes in the data hash. Save the index numbers where the expocodes #--> match in an array. Then pass directory, expocode(s), and matching array #--> to HTML creation script. # #in list of expos.. some codes are in the format "blah_1" while in the database #they are just "blah" causing entries not to show up. #for($i=36; $i<=36 ; $i++){ $numfiles = 0; for($i=0; $i<=$#expo_buffer ; $i++){ if(grep(/$expo_hash{'Use'}[$i]/, "true")) { $numfiles++;######## # print "$i||$expo_hash{'Line'}[$i]||$expo_hash{'Directory'}[$i]||$expo_hash{'Expocode'}[$i]||$expo_hash{'Use'}[$i]\n"; @expocodes = split(/ and /, $expo_hash{'Expocode'}[$i]); @matches = (); foreach $thing (@expocodes) { for($j=0; $j<=$num_of_entries ; $j++){ if(grep(/^$thing$/, $data_hash{'ExpoCode'}[$j])){ # print"$j\n"; # add $j to array of results push(@matches, $j); } } } # print "@matches\n"; *arr=\@matches; #call HTML creation script print $numfiles;######### &html_creation($expo_hash{'Line'}[$i], $expo_hash{'Directory'}[$i], $expo_hash{'Expocode'}[$i], *arr); } }