#!/usr/bin/perl # #Program name: stockchart.pl # #Description: Take a range of records, comma-delimited, of yahoo stock # quotes for a range of dates. Create an HTML chart from the data. # #Author: Noe Dinnerstein, BodhiSoftWare, Inc. # #******************************************************************************** #=========================================================== # VARIABLE DECLARATIONS #=========================================================== $save_mmmyy=" "; $save_symbol=" "; $ssym=" "; $mmmyy=" "; $firstHeader=1; #Flag #Hash table of months and their numbers %month=('Jan','01','Feb','02','Mar','03','Apr','04','May','05','Jun','06','Jul','07','Aug','08','Sep','09','Oct','10','Nov','11','Dec','12'); #========================================================================== #open input file #open output HTML file #========================================================================== open (INDAT,"yahoo_stocks.html") or die("Error opening output"); #================================================= #We must read the data into an array #in order to read it backwards out of the array #to get it in the right order. #==================================================== @DataArray=""; #Array of data from the file $idx=0; #Array index while () { $DataArray[$idx]=$_; $idx++; } if ($idx == 0) {exit(0)}; #============================================== $idx--; #we went one too far at end of file, #so decrement counter by 1 #================================================ #write HTML header beginPage(); #======== Now read it back data ========== for ($i=$idx;$i > -1;--$i){ #print range in reverse $idx to 0 @PartArray=split /,/,$DataArray[$i]; $ssym=$PartArray[0]; #get rid of new line #if there is a change of stock symbol if ($ssym ne $save_symbol) { if ($firstHeader == 0) { writeFooter(); } #end if else { $firstHeader = 0;} writeHeader(); $save_symbol=$ssym; } #end if writeData(); } #end for-loop writeFooter(); writeEndFooter(); close INDAT; close HTML; exit(0); #============================================== # SUBROUTINES #============================================== # # set up HTML page #============================================== sub beginPage { print HTML "\n"; print HTML "\n"; print HTML "

Perl Demo

\n"; print HTML "

Stock quotes charts generated from yahoo and formatted by Perl programs.

\n"; } #================================================== # headers are done here #================================================= sub writeHeader { print "writeHeader\n"; #print HTML "\n"; print HTML "\n"; print HTML "
\n"; print HTML "\n"; print HTML "\n"; print HTML "

STOCK PRICES

SYMBOL:$ssym
\n"; print HTML "\n"; #print HTML "\n"; print HTML "\n"; } #================================================ # detail cells #================================================ sub writeData { write "writeData\n"; @td=split /,/, $DataArray[$i]; $sdate=$td[1]; $sclose=$td[5]; @dpart=split /-/,$sdate; $smmm=$dpart[1]; $dmm=$month{$smmm}; $sdd=$dpart[0]; $syy=$dpart[2]; $disp_mmmyy=" "; $mmmyy="$syy$dmm"; #print "save_mmmyy:$save_mmmyy, mmmyy:$mmmyy\n"; if ($save_mmmyy ne $mmmyy) { if ($save_mmmyy ne " ") { print HTML "\n"; print HTML "\t \n"; #print HTML "
\n"; print HTML "\t
\n"; #print HTML "\t \n"; } $disp_mmmyy = $mmmyy; $save_mmmyy = $mmmyy; print HTML "\t \n"; print HTML "\t\n"; print HTML "\t\n"; } else {$disp_mmmyy = " ";} print HTML "\n"; print HTML "\t \t"; print HTML "\n"; } #================================================ # end of individual stock sections #================================================ sub writeFooter { print "writeFooter\n"; print HTML "\n"; print HTML "\t\n"; print HTML "\t
$smmm
\n"; print HTML "\t \t \t\n"; print HTML "\t \t \t \n"; print HTML "\t \t \t \n"; print HTML qq(\t \t \t \n); print HTML "\t \t \t
$sdd
$sclose
\n"; print HTML "\t \t \t
\n"; print HTML "\n"; print HTML " \n"; print HTML " \n"; #print HTML "

\n"; print HTML "





\n"; } #=============================================== # Final footer of the HTML page #=============================================== sub writeEndFooter { print "writeEndFooter\n"; print HTML "\n"; print HTML "\n"; print HTML "\n"; } #==================================================