# Replacement for StepOne's worksheet
#
# Rev 0-2 22Jun11 Added capability for openoffice
# Rev 0-3 27Jul11 Added reserved columns to column AZ
# Rev 1.0 16Dec11 Changed source of historic dividends from Morningstar to Dividend Investor
# Rev 1.1 01May13 Added dump of data_hash
#				Replaced morningstar with Google for number of shares in issue
#
# %data_hash
#	key = epic
#	0	= Name
#	1	= ISIN
#	2	= EPIC
#	3	= Price
#	4	= H P/E
#	5	= F P/E
#	6	= H Yield
#	7	= F Yield
#	8	= PTBV
#	9	= Gearing
#	10	= EPS Growth
#	11	= F Cover
#	12	= PSR
#	13	= Inc Div
#	14	= Cap (Łm)
#	15	= Index
#	16	= Sector
#	17	= Last Results
#	18	= EPS
#	19	= Feps1
#	20	= Feps2
#	21	= Div 1 current year - 4 (2007)
#	22	= Div 2 current year - 3 (2008)
#	23	= Div 3 current year - 2 (2009)
#	24	= Div 4 current year - 1 (2010)
#	25	= Div current year(2011)
#	26	= Fdiv1
#	27	= Fdiv2
#	28	= Book
#	29	= Intangibles
#	30	= Cash
#	31	= Turnover
#	32	= Number of shares
#	33	=
	
use strict;
use warnings;

use WWW::Mechanize;
use HTML::TreeBuilder;
use HTML::TableExtract;

my ($url, $url_signon, $content, $dummy, $f_htmlfile1, $f_htmlfile2, $response, $browser,
	%data_hash, %epic_hash, $f_epic, $epic, $name, $f_output_excel, $f_output_oo, $eps_growth,
	$sector, $index, $tree, $text, $ac, @history, $row_cnt, $te, $ts,
	@x, $row, @forecast, $current_date, @date_time, $yyyymmdd, @data_array,
	$f_ftse350, $isin, $lse_seg, $lse_sector, $lse_subsector, $mstar_id,
	$current_year, $key, $start_time, $end_time, $duration, %fund_hash, $mytime, $f_hashdump
);


$f_htmlfile1 	= 'html1.txt';						# debug data
$f_htmlfile2 	= 'html2.txt';						# debug data
$f_ftse350 		= 'ftse350.xls';					# list of FTSE350 stocks
$f_epic			= 'epic.txt';
$f_hashdump		= 'data_hash.txt';
$f_output_excel	= 'stepone_replacement_base.xls';		# tab-delimited output file for Excel
$f_output_oo	= 'stepone_replacement_base_oo.xls';	# tab-delimited output file for openoffice
$start_time 	= time();

# get date-time data
	@date_time = gmtime();
	$current_date = (1900 + $date_time[5]).sprintf("%02d",($date_time[4]+1)).sprintf("%02d",($date_time[3]));
	$current_year = 1900 + $date_time[5];
	

#Read list of epics
	&read_epic;		# from ftse350.xls

#goto TEMP_START;

# Now get financial data

	# EPS, EPS growth, turnover
	print "Processing Financial_1\n";
	&get_financial_1;

	# Historic dividends
	#print "Processing Financial_1a\n";
	#&get_financial_1a;			>>>>>>>>>>>>>>>>>>>>>>>>>>>>>DISABLED
	
	# Historic dividends
	print "Processing Financial_1b\n";
	&get_financial_1b;
TEMP_START:
	# forecast dividend, P/E, EPS, dividend cover, market cap
	print "Processing Financial_2\n";
	&get_financial_2;

	# PTBV, PSR, Gearing, PER
	print "Processing Financial_3\n";
	&get_financial_3;

	# Intangible
	print "Processing Financial_4\n";
	&get_financial_4;
TEMP:		
	# Last results
	print "Processing Financial_5\n";
	&get_financial_5;
	
	# Shares in issue
	print "Processing Financial_6a\n";
	&get_financial_6a;
	
	# Process funds....Dividends, last Price, Gearing, No of shares
	print "Processing Financial 7\n";
	&get_financial_7;
	
	# Process funds....Last Results
	print "Processing Financial 8\n";
	&get_financial_8;

	# Compute calculated columns for excel
	print "Processing calc_col_excel\n";
	&calc_col_excel;
	
	# Print data to file
	&print_output($f_output_excel);
	
	# Compute calculated columns for openoffice
	print "Processing calc_col_oo\n";
	&calc_col_oo;
	
	# Print data to file
	&print_output($f_output_oo);

END_OF_PROCESS:
$end_time = time();
$duration = ($end_time - $start_time)/60;
print "Finished    $duration mins\n";


#######################################################################################################
########## subroutines    #############################################################################
#######################################################################################################

###########################################
### start of read_epic
### reads list of EPIC codes from file and write to hash
sub read_epic {
	my ($record, @row, $fund, $yahoo_symbol, $yahoo_name, $url, $divind_id);
	open INFILE, $f_ftse350;
	<INFILE>;
	while (<INFILE>){
		$record = $_;
		chomp($record);
		($epic,$name,$index,$isin,$lse_seg,$lse_sector,$lse_subsector,$mstar_id,$yahoo_symbol,$yahoo_name, $fund, $url, $divind_id) = split("\t", $record);
		$epic_hash{$epic}=[$name,$index,$isin,$lse_seg,$lse_sector,$lse_subsector,$mstar_id,$yahoo_symbol,$yahoo_name,$fund,$url, $divind_id];
	}
	close INFILE;
	
	# for debug purposes... delete unwanted epics
	#foreach $key (sort keys %epic_hash){
	#	unless ($key eq 'REX' or $key eq 'AGK' or $key eq 'SSE' or $key eq 'LLOY' or $key eq 'SCIN'
	#			){delete $epic_hash{$key}}
	#}
	
	foreach $key (sort keys %epic_hash){
		$data_hash{$key}[2] = $key;
	}
	
}
### end of read_epic
###########################################


###########################################
### start of get financial data 1
### EPS, EPS growth
### Morningstar/Financials/Summary
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=fs&SecurityToken=0P00007OUT]3]0]E0WWE$$ALL&Id=0P00007OUT&ClientFund=0&CurrencyId=GBP
sub get_financial_1 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $first_year,
		$turnover, $key_count
	);
	#&read_data_hash;
	$browser = WWW::Mechanize->new(autocheck => 0);
	
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=fs&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		$browser = WWW::Mechanize->new(autocheck => 0);
		$key_count++;
		$url = $url_base;
		$te = HTML::TableExtract->new();
		$content = '';
		$ts = '';
		$first_year = '';
		$mytime = localtime();
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;
		$data_hash{$key}[0] = $epic_hash{$key}[0];	# Name
		$data_hash{$key}[1] = $epic_hash{$key}[2];	# ISIN
		$data_hash{$key}[2] = $key;					# EPIC
		$data_hash{$key}[15] = $epic_hash{$key}[1];	# index
		$data_hash{$key}[16] = $epic_hash{$key}[4];	# Sector
		
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		
		RETRY_FIN1:
		print "  1 $key_count    $key   ";
		unless ($browser->get($url)){
			sleep 5;
			goto RETRY_FIN1;
		}
		
		$content = $browser->content;
		
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		open OUTFILE, ">temp1.txt";		# for debug purposes
			print OUTFILE "$content";
		close OUTFILE;
		use warnings;
		
		$te->parse($content);
		
		# check if a fund
		$content =~s/\n//g;
		if ($content =~/Fund Report/){$fund_hash{$key} = 1}
		
		
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
		   $dummy = 1;
			
			no warnings;
			foreach $row ($ts->rows) {
				@data = undef;
		   		@data = @$row;
		   		#print join(',', @$row), "\n";
		   		$turnover = '';
		   		if (@$row[0] and @$row[1] =~ /2\d\d\d/){		# find year
		   			$first_year = @$row[1];
		   		}
		   		
		   		if (@$row[0] eq 'Normalized EPS p' and $#data == 2 and @$row[2] =~/\d/){
		   			$data_hash{$key}[18] = @$row[2];                     		# EPS
		   			if (@$row[1] ne '-' and @$row[1] ne '0.00'){$eps_growth = 100*((@$row[2] / @$row[1])-1)}  	# EPS growth
		   			$data_hash{$key}[10] = sprintf("%.1f", $eps_growth); 		# EPS growth
		   		} elsif (@$row[0] eq 'Normalized EPS p' and $#data == 5 and @$row[5] =~/\d/){
		   			$data_hash{$key}[18] = @$row[5];                     		# EPS
		   			if (@$row[4] ne '-'){$eps_growth = 100*((@$row[5] / @$row[4])-1)}	# EPS growth
		   			$data_hash{$key}[10] = sprintf("%.1f", $eps_growth); 		# EPS growth
		   		}
		   		if (@$row[0] eq 'Dividend Yield %'){
		   				if (@$row[5] eq '-'){
		   					$data_hash{$key}[6] = @$row[4];                      # Hist Yield
		   				} else {
		   					$data_hash{$key}[6] = @$row[5];                      # Hist Yield
		   				}
		   		}
		   		if (@$row[0] eq 'Turnover' and $#data == 2 and (@$row[2] =~/\d/)){
		   			$turnover = @$row[2];
		   			$turnover =~s/,//g;
		   			if($turnover ne '-'){$data_hash{$key}[31] = sprintf("%d",$turnover)}              # Turnover
		   		} elsif (@$row[0] eq 'Turnover' and $#data == 5 and @$row[5] =~/\d/){
		   			$turnover = @$row[5];
		   			$turnover =~s/,//g;
		   			if($turnover ne '-'){$data_hash{$key}[31] = sprintf("%d",$turnover)}              # Turnover
		   		}
			}
			use warnings;
		}
		print "$data_hash{$key}[0] $data_hash{$key}[31]\t\t\t$mytime\n";
	}	# end of foreach $key

	&dump_data_hash;
	undef %data_hash;

}
### end of get financial data 1
###########################################


###########################################
### start of get financial data 1a
### Historic dividends
### 
### http://dividendinvestor.co.uk/historical.php?no=2757
sub get_financial_1a {
	
	my ($browser, $url_base, $te, @data, $key, $ts, $row, $content, $year, $div
	);
	
	$url_base = 'http://www.dividendinvestor.co.uk/historical.php?no=';
	
	&read_data_hash;
	
	foreach $key (sort keys %epic_hash){
		$browser = WWW::Mechanize->new(autocheck => 0);
		$te = HTML::TableExtract->new();
		print "$key\n";
		$ts = '';
		$url = $url_base.$epic_hash{$key}[11];
		$mytime = localtime();
		
		RETRY_FIN1:
		unless ($browser->get($url)){
			sleep 5;
			goto RETRY_FIN1;
		}
		$content = '';
		$content = $browser->content;
			
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		#open OUTFILE, ">temp1.txt";		# for debug purposes
		#	print OUTFILE "$content";
		#close OUTFILE;
		use warnings;
		
		$te->parse($content);
		$ts = '';	
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
			$row = undef;
			no warnings;
			foreach $row ($ts->rows) {
				$year = undef;
				$div = undef;
				@data = undef;
		   		@data = @$row;
		   		#print join(',', @$row), "\n";
		   		if ($data[0] =~ /(\d\d\d\d) Total:/){
		   			$year = $1;
		   			$div = $data[5];
		   			#$div =~ s/ //g;
		   			
		   			# write dividend to data hash
		   			if ($year == $current_year) {
		   				$data_hash{$key}[25] = $div;
		   			} elsif ($year == $current_year - 1) {
		   				$data_hash{$key}[24] = $div;
		   			} elsif ($year == $current_year - 2) {
		   				$data_hash{$key}[23] = $div;
		   			} elsif ($year == $current_year - 3) {
		   				$data_hash{$key}[22] = $div;
		   			} elsif ($year == $current_year - 4) {
		   				$data_hash{$key}[21] = $div;
		   			}
		   			
		   			$dummy = 1;
		   		}
			}	# end of foreach row
			use warnings;
			$dummy = 1;
		}	# end of foreach ts
	} # end of foreach keys
	
	
	&dump_data_hash;
	undef %data_hash;
	
	
}
### end of get financial 1a
###########################################


###########################################
### start of get financial data 1b
### Historic dividends
### 
### http://dividendinvestor.co.uk/historical.php?exg=GB&symbol=NG.
sub get_financial_1b {
	
	my ($browser, $url_base, $te, @data, $key, $ts, $row, $content, $year, $div
	);
	
	$url_base = 'http://dividendinvestor.co.uk/historical.php?exg=GB&symbol=';
	
	&read_data_hash;
	
	foreach $key (sort keys %epic_hash){
		$browser = WWW::Mechanize->new(autocheck => 0);
		$te = HTML::TableExtract->new();
		print "1b  $key       ";
		$ts = '';
		$url = $url_base.$key;
		$mytime = localtime();
		
		RETRY_FIN1:
		unless ($browser->get($url)){
			sleep 5;
			goto RETRY_FIN1;
		}
		$content = '';
		$content = $browser->content;
			
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		#open OUTFILE, ">temp1.txt";		# for debug purposes
		#	print OUTFILE "$content";
		#close OUTFILE;
		use warnings;
		
		$te->parse($content);
		$ts = '';	
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
			$row = undef;
			no warnings;
			foreach $row ($ts->rows) {
				$year = undef;
				$div = undef;
				@data = undef;
		   		@data = @$row;
		   		#print join(',', @$row), "\n";
		   		if ($data[0] =~ /(\d\d\d\d) Total:/){
		   			$year = $1;
		   			$div = $data[5];
		   			#$div =~ s/ //g;
		   			$div =~ s/\n//g;
		   			
		   			# write dividend to data hash
		   			if ($year == $current_year) {
		   				$data_hash{$key}[25] = $div;
		   			} elsif ($year == $current_year - 1) {
		   				$data_hash{$key}[24] = $div;
		   			} elsif ($year == $current_year - 2) {
		   				$data_hash{$key}[23] = $div;
		   			} elsif ($year == $current_year - 3) {
		   				$data_hash{$key}[22] = $div;
		   			} elsif ($year == $current_year - 4) {
		   				$data_hash{$key}[21] = $div;
		   			}
		   			
		   			$dummy = 1;
		   		}
			}	# end of foreach row
			use warnings;
			$dummy = 1;
		}	# end of foreach ts
		print "$data_hash{$key}[21]   $data_hash{$key}[22]    $data_hash{$key}[23]    $data_hash{$key}[24]    $data_hash{$key}[25]\t\t\t$mytime\n";
		sleep 10;
	} # end of foreach keys
	
	
	&dump_data_hash;
	undef %data_hash;
	
}
### end of get financial 1b
###########################################


###########################################
### start of get financial data 2
### forecast dividend, P/E, EPS, dividend cover, market cap, number of shares, last price
### Morningstar/Overview/Summary
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=0&vw=sum&SecurityToken=0P00007NWB]3]0]E0WWE$$ALL&Id=0P00007NWB&ClientFund=0&CurrencyId=GBP
sub get_financial_2 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $y1, $y2, $div_cover,
		$content_base, $mcap, $unit, $name, $ticker, $fcst_yr1, $fcst_yr2, $net_profit, $reported_eps,
		$last_price, $browse_count, $key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=0&vw=sum&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		$mytime = localtime();
		
		$browser = WWW::Mechanize->new(autocheck => 0);
		$key_count++;
		print "  2 $key_count   $key\t\t\t$mytime\t";
		$content = '';
		$net_profit= 0;
		$reported_eps = 0;
		$fcst_yr1 = '';
		$fcst_yr2 = '';
		
		
		$te = HTML::TableExtract->new();
		$url = $url_base;
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;
		#print "$url\n";
		
		$data_hash{$key}[0] = $epic_hash{$key}[0];	# Name
		$data_hash{$key}[2] = $key;					# EPIC
		$data_hash{$key}[15] = $epic_hash{$key}[1];	# index
		
		$browse_count = 0;
		RETRY_FIN2:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN2;
		}
		$content = $browser->content;
		
		$te->parse($content);
		
		$content =~s/\n//g;
		$content =~s/"//g;
		$content_base = $content;
		$content =~/.*currencySymbol(.*?)span>(.*?)<.*/;		# Find Last Price
#		$last_price = $2;
#		$last_price =~s/,//g;
#		$data_hash{$key}[3] = $last_price;
#		print "$last_price\n";
		
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
		   $dummy = 1;
		   
		   foreach $row ($ts->rows) {
		   		unless (@$row[0]){next}
		   		@data = @$row;
		   		
		   		if (@$row[0] eq 'Net Profit'){
		   			if (@$row[3] eq '-'){
		   				$net_profit = @$row[2];
		   			} else {
		   				$net_profit = @$row[3];
		   			}
		   		}
		   		if (@$row[0] eq 'Reported EPS'){
		   			if (@$row[3] eq '-'){
		   				$reported_eps = @$row[2];
		   			} else {
		   				$reported_eps = @$row[3];
		   			}
		   		}
		   		

		   		if(@$row[0] =~/2\d\d\d/ and $#data == 7){
		   			if (@$row[0] == $current_year){
		   				$data_hash{$key}[19] = @$row[2];	# EPS Y1
		   				$data_hash{$key}[5]  = @$row[4];   # future P/E
						$data_hash{$key}[26] = @$row[6];   # dividend Y1
						$data_hash{$key}[7]  = @$row[7];   # yield Y1
		   			}
		   			if (@$row[0] == $current_year+1){
		   				$data_hash{$key}[20] = @$row[2];	# EPS Y2
		   				$data_hash{$key}[5]  = @$row[4];   # future P/E
						$data_hash{$key}[27] = @$row[6];   # dividend Y2
						$data_hash{$key}[7]  = @$row[7];   # yield Y2
		   			}
		   		}
		   }	# end of foreach $row
		}		# end of foreach $ts
		
		print "Reported EPS = $reported_eps\n";
		
		if ($data_hash{$key}[26] and $data_hash{$key}[26] ne '-'){$div_cover = $data_hash{$key}[19]/$data_hash{$key}[26]}		# future dividend cover
		if ($div_cover){$data_hash{$key}[11]  = sprintf("%.1f", $div_cover);}					# future dividend cover
		
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		open OUTFILE, ">temp.txt";
		print OUTFILE "$content";
		close OUTFILE;
		use warnings;
		
	}	# end of foreach $key
	
	&dump_data_hash;
	undef %data_hash;

}
### end of get financial data 2
###########################################


###########################################
### start of get financial data 3
### PTBV, PSR, PER
### Morningstar/Key Ratios
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=kr&SecurityToken=0P00007NWB]3]0]E0WWE$$ALL&Id=0P00007NWB&ClientFund=0&CurrencyId=GBP
sub get_financial_3 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $browse_count,
		$key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=kr&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		$mytime = localtime();
		
		$key_count++;
		$browser = WWW::Mechanize->new(autocheck => 0);
		print "  3 $key_count   $key\t\t\t$mytime\n";
		$url = $url_base;
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;
		$te = HTML::TableExtract->new();
		RETRY_FIN3:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN3;
		}
		$content = $browser->content;
		
		$te->parse($content);
		
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
		   $dummy = 1;
			
			no warnings;
			foreach $row ($ts->rows) {
		   		@data = @$row;
		   		if (@$row[0] eq 'Price to Tangible Book Value'){
		   			$data_hash{$key}[8] = @$row[2];   # PTBV
		   		}
		   		if (@$row[0] eq 'Price/Sales'){
		   			$data_hash{$key}[12] = @$row[2];   # PSR
		   		}
		   		if (@$row[0] eq 'Net Gearing'){
		   			if ($#data == 4){		# to ensure we pick up the correct occurrence
		   				$data_hash{$key}[9] = @$row[2];   # Gearing
		   			}
		   		}
		   		if (@$row[0] eq 'PER (E)'){
		   				$data_hash{$key}[4] = @$row[2];   # PER(E)
		   		}
		   		
		   		
		   		
			}
			use warnings;
		}
	}

	&dump_data_hash;
	undef %data_hash;

}
### end of get financial data 3
###########################################


###########################################
### start of get financial data 4
### Intangibles, Cash, total assets
### Morningstar/Financials/Balance Sheet
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=bs&SecurityToken=0P00007OUT]3]0]E0WWE$$ALL&Id=0P00007OUT&ClientFund=0&CurrencyId=GBP
sub get_financial_4 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $tot_assets, $intangible,
		$cash, $browse_count, $key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=10&vw=bs&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		$mytime = localtime();
		
		$key_count++;
		$browser = WWW::Mechanize->new(autocheck => 0);
		print "  4  $key_count  $key\t\t\t$mytime\n";
		$tot_assets = '';
		$intangible = '';
		$cash = '';
		$url = $url_base;
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;
		$te = HTML::TableExtract->new();
		RETRY_FIN4:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN4;
		}
		$content = $browser->content;
		
		$te->parse($content);
		
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
		   $dummy = 1;
			
			no warnings;
			foreach $row ($ts->rows) {
		   		@data = @$row;
		   		if (@$row[0] eq 'Intangible'){
		   			if (@$row[5] ne '-'){
		   				$intangible = @$row[5];
		   			} else {
		   				$intangible = @$row[4];
		   			}
		   			$intangible =~s/,//g;
		   			if($intangible){$data_hash{$key}[29] = sprintf ("%d",$intangible);}   # Intangible
		   		}
		   		if (@$row[0] eq 'Cash and Securities'){
		   			if ($key eq 'BT.A'){
		   				$dummy = 1;
		   			}
		   			if (@$row[5] ne '-'){
		   				$cash = @$row[5];
		   			} else {
		   				$cash = @$row[4];
		   			}
		   			$cash =~s/,//g;
		   			if ($cash){$data_hash{$key}[30] = sprintf ("%d",$cash);}   			# Cash
		   		}
		   		if (@$row[0] eq 'Total Assets'){
		   			if (@$row[5] ne '-'){
		   				$tot_assets = @$row[5];
		   			} else {
		   				$tot_assets = @$row[4];
		   			}
		   			$tot_assets =~s/,//g;
		   			if ($tot_assets){$data_hash{$key}[28] = sprintf ("%d",$tot_assets);}   # Total assets
		   		}
		   		
			}
			use warnings;
		}
	}

	&dump_data_hash;
	undef %data_hash;

}
### end of get financial data 4
###########################################


###########################################
### start of get financial data 5
### Last Results
### Morningstar/News and Events/Key Dates
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=3&vw=kd&SecurityToken=0P00007NWB]3]0]E0WWE$$ALL&Id=0P00007NWB&ClientFund=0&CurrencyId=GBP
sub get_financial_5 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $last_results, $announce_type,
		$browse_count, $key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=3&vw=kd&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		$mytime = localtime();
		
		$key_count++;
		$browser = WWW::Mechanize->new(autocheck => 0);
		print "  5 $key_count    $key    ";
		$url = $url_base;
		$last_results = '';
		$announce_type = '';
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;
		print "$url\t\t\t$mytime\n";
		$te = HTML::TableExtract->new();
		RETRY_FIN5:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN5;
		}
		$content = $browser->content;
		
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		open OUTFILE, ">temp.txt";
		print OUTFILE "$content";
		close OUTFILE;
		use warnings;
		
		$te->parse($content);
		
		foreach $ts ($te->tables) {
		   #print "Table (", join(',', $ts->coords), "):\n";
		   $dummy = 1;
			$last_results = '';			
			no warnings;
			foreach $row ($ts->rows) {
		   		@data = @$row;
		   		if (@$row[0] =~/Last.*announcement/){		# Look for last results
		   			unless ($last_results){
			   			@$row[0] =~/Last(.*)announcement/;
			   			$announce_type = $1;
			   			$last_results = @$row[1];
			   			if ($announce_type =~ /interim/){
			   				$data_hash{$key}[17] = $last_results.'(interim)';   # Last Results
			   			} elsif ($announce_type =~ /prelim/){
			   				$data_hash{$key}[17] = $last_results.'(prelim)';   # Last Results
			   			} elsif ($announce_type =~ /Q/){
			   				$data_hash{$key}[17] = $last_results.'('.$announce_type.')';   # Last Results
			   			}
		   			}
		   		}	# end of if (@$row[0]
			}	# end of foreach $row
			use warnings;
		} # end of foreach $ts
		print "$data_hash{$key}[17]\n";
	}	# end of foreach $key

	&dump_data_hash;
	undef %data_hash;
	
}
### end of get financial data 5
###########################################


###########################################
### start of get financial data 6
### Shares in issue
### Morningstar/Share Price/Summary
### http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=2&vw=sum&SecurityToken=0P00007O1O]3]0]E0WWE$$ALL&Id=0P00007O1O&ClientFund=0&CurrencyId=GBP
sub get_financial_6 {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $num_shares, $browse_count,
		$key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/stockreport/default.aspx?tab=2&vw=sum&SecurityToken=mstar_id]3]0]E0WWE$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		
		$browser = WWW::Mechanize->new(autocheck => 0);
		$key_count++;
		print "  6  $key_count  $key\n";
		$url = $url_base;
		$num_shares = '';
		
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;

		RETRY_FIN6:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN6;
		}
		$content = $browser->content;
		
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		open OUTFILE, ">temp.txt";
		print OUTFILE "$content";
		close OUTFILE;
		use warnings;
		
		$content =~s/\n//g;

		$content =~/.*Issue.*?(\d*,*\d+m)<.*/;		# Find number of shares in issue
		$num_shares = $1;
		if ($num_shares){
			$num_shares =~s/,//g;
			$num_shares =~s/m//g;
			$data_hash{$key}[32] = $num_shares;
		}

	}		# end of foreach $key

	&dump_data_hash;
	undef %data_hash;
	
}
### end of get financial data 6
###########################################

###########################################
### start of get financial data 6a
### Shares in issue
### Replaces original which got data from Morningstar (seems to have excluded data from 01May13)
### https://www.google.com/finance?authuser=0&q=LON%3AAGK&ei=70SCUZDELeiPwAOffw
sub get_financial_6a {
	
	my ($key, $browser, $url_base, $url, $content, $te, $ts, $rows, @data, $num_shares, $browse_count,
		$key_count
	);
	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'https://www.google.com/finance?authuser=0&q=LON%3AEPIC&ei=70SCUZDELeiPwAOffw';
	
	&read_data_hash;
	
	$key_count = 0;
	foreach $key (sort keys %epic_hash){
		#check if a fund
		if ($epic_hash{$key}[9] eq "Yes") {next}
		
		$browser = WWW::Mechanize->new(autocheck => 0);
		$key_count++;
		print "  6  $key_count  $key\n";
		$url = $url_base;
		$num_shares = '';
		
		$url =~ s/EPIC/$key/g;

		RETRY_FIN6a:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN6a;
		}
		$content = $browser->content;
		$content =~s/\n//g;
		no warnings;		# to suppress "Wide character in print" warning (naughty!)
		open OUTFILE, ">temp.txt";
		print OUTFILE "$content";
		close OUTFILE;
		use warnings;
		
		$content =~s/\n//g;
		$content =~s/"//g;

		$content =~/.*>Shares.*?(\d*\.\d+[BM])<.*/;		# Find number of shares in issue
		$num_shares = $1;
		if ($num_shares){
			$num_shares =~s/,//g;
			if ($num_shares =~/B/){
				$num_shares =~s/B//g;
				$num_shares = $num_shares * 1000;
			} else {
				$num_shares =~s/M//g;
			}
			$data_hash{$key}[32] = $num_shares;
		}

	}		# end of foreach $key

	&dump_data_hash;
	undef %data_hash;
	
}
### end of get financial data 6a
###########################################


###########################################
### start of get financial data 7
### Funds data.... last Price, Gearing, No of shares
### 
### http://tools.morningstar.co.uk/uk/cefreport/default.aspx?Site=uk&id=F0GBR04E7U&LanguageId=en-GB&SecurityToken=F0GBR04E7U]2]0]FCGBR$$ALL
### 

	#	key = epic
	#	0	= Name			-
	#	1	= ?				-
	#	2	= EPIC			-
	#	3	= Price			Funds/Overview
	#	4	= H P/E			-
	#	5	= F P/E			-
	#	6	= H Yield		-
	#	7	= F Yield		-
	#	8	= PTBV			-
	#	9	= Gearing		Funds/Overview
	#	10	= EPS Growth	-
	#	11	= F Cover		-
	#	12	= PSR			-
	#	13	= Inc Div		-
	#	14	= Cap (Łm)		Funds/Overview
	#	15	= Index			-
	#	16	= Sector		-
	#	17	= Last Results	Funds/Key Dates
	#	18	= EPS			-
	#	19	= Feps1			-
	#	20	= Feps2			-
	#	21	= Div 1 current year - 4 (2007)		Funds/Overview
	#	22	= Div 2 current year - 3 (2008)		Funds/Overview
	#	23	= Div 3 current year - 2 (2009)		Funds/Overview
	#	24	= Div 4 current year - 1 (2010)		Funds/Overview
	#	25	= Div current year(2011)			Funds/Overview
	#	26	= Fdiv1			-
	#	27	= Fdiv2			-
	#	28	= Book			-
	#	29	= Intangibles	-
	#	30	= Cash			-
	#	31	= Turnover		-
	#	32	= Number of shares		Funds/Overview
sub get_financial_7 {
	my($browser, $content, $key_count, $url_base, $url, $browse_count, $te, $ts, $row, $num_shares,
	   %div_hash
	);

	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/cefreport/default.aspx?tab=0&vw=sum&SecurityToken=mstar_id]2]0]FCGBR$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	no warnings;
	foreach $key (sort keys %epic_hash){
		if ($epic_hash{$key}[9] eq "No"){next}		# step to next key if not fund
		
		$key_count++;
		$num_shares = '';
		undef(%div_hash);
		print "  7  $key_count  $key\n";
		
		$url = $url_base;
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;		# create url
		$browser = WWW::Mechanize->new(autocheck => 0);
		
		RETRY_FIN7:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN7;
		}
		$content = $browser->content;
		
		$te = HTML::TableExtract->new();
		$te->parse($content);
		
		foreach $ts ($te->tables) {
			foreach $row ($ts->rows) {
					$num_shares = '';
					#print join(',', @$row), "\n";
					
					if (@$row[0] eq 'Last Close'){
						chomp(@$row[1]);
			   			$data_hash{$key}[3] = @$row[1];	# Last price
			   			$data_hash{$key}[3] =~s/,//g;
		   			} 
		   			if (@$row[0] eq 'Net Gearing'){
			   			$data_hash{$key}[9] = @$row[1];	# Net Gearing
			   			$data_hash{$key}[9] =~s/,//g;
		   			} 
					if (@$row[0] eq 'Ordinary Share'){
						$num_shares = @$row[1];
						$num_shares =~/(\d+\.*\d*).*/;
						$num_shares = $1;
			   			$data_hash{$key}[32] = $num_shares;	# number of shares
			   			$data_hash{$key}[32] =~s/,//g;
		   			} 
		   			if ((@$row[0] =~/Income/ or @$row[0] =~/Capital/) and @$row[3] =~/2\d\d\d/){	# identify dividends
			   			$div_hash{@$row[3]} += @$row[1];
		   			} 
					
			   }  # end of foreach $row
		}  # end of foreach $ts
		
		#Following block was used when dividends came from morningstar. Now sourced from Dividend Investor
		#in get_financial_1a
		#$data_hash{$key}[21] = $div_hash{$current_year-4};
		#$data_hash{$key}[22] = $div_hash{$current_year-3};
		#$data_hash{$key}[23] = $div_hash{$current_year-2};
		#$data_hash{$key}[24] = $div_hash{$current_year-1};
		#$data_hash{$key}[25] = $div_hash{$current_year};
		
	}	# end of foreach $key
	use warnings;

	&dump_data_hash;
	undef %data_hash;
}
### end of get financial data 7
###########################################





###########################################
### start of get financial data 8
### Funds data.... Last Results
### 
### http://tools.morningstar.co.uk/uk/cefreport/default.aspx?tab=0&vw=kd&SecurityToken=E0GBR00F4W]2]0]FCGBR$$ALL&Id=E0GBR00F4W&ClientFund=0&CurrencyId=GBP
###

sub get_financial_8 {
	my($browser, $content, $key_count, $url_base, $url, $browse_count, $te, $ts, $row, 
	   $last_results, $announce_type
	);

	
	$browser = WWW::Mechanize->new(autocheck => 0);
	$url_base = 'http://tools.morningstar.co.uk/uk/cefreport/default.aspx?tab=0&vw=kd&SecurityToken=mstar_id]2]0]FCGBR$$ALL&Id=mstar_id&ClientFund=0&CurrencyId=GBP';
	
	&read_data_hash;
	
	$key_count = 0;
	no warnings;
	foreach $key (sort keys %epic_hash){
		if ($epic_hash{$key}[9] eq "No"){next}		# step to next key if not fund
		
		$key_count++;
		$last_results = '';
		$announce_type = '';
		print "  8  $key_count  $key\n";
		
		$url = $url_base;
		$url =~ s/mstar_id/$epic_hash{$key}[6]/g;		# create url
		$browser = WWW::Mechanize->new(autocheck => 0);
		
		RETRY_FIN7:
		unless ($browser->get($url)){
			print "$browse_count\n";
			sleep 5;
			$browse_count++;
			if ($browse_count > 5) {last}
			goto RETRY_FIN7;
		}
		$content = $browser->content;
		
		$te = HTML::TableExtract->new();
		$te->parse($content);
		
		foreach $ts ($te->tables) {
			foreach $row ($ts->rows) {
					if (@$row[0] =~/Last.*announcement/){		# Look for last results
		   			unless ($last_results){
			   			@$row[0] =~/Last(.*)announcement/;
			   			$announce_type = $1;
			   			$last_results = @$row[1];
			   			if ($announce_type =~ /interim/){
			   				$data_hash{$key}[17] = $last_results.'(interim)';   # Last Results
			   			} elsif ($announce_type =~ /prelim/){
			   				$data_hash{$key}[17] = $last_results.'(prelim)';   # Last Results
			   			} elsif ($announce_type =~ /Q/){
			   				$data_hash{$key}[17] = $last_results.'('.$announce_type.')';   # Last Results
			   			}
		   			}
		   		}	# end of if (@$row[0]
					
			   }  # end of foreach $row
		}  # end of foreach $ts
		
	}	# end of foreach $key
	use warnings;

	&dump_data_hash;

}
### end of get financial data 8
###########################################



###########################################
### Start of calculated columns for excel
sub calc_col_excel {
	my ($key, $row);
	
	$row = 2;
	no warnings;
	foreach $key (sort keys %data_hash){
		$data_hash{$key}[4] = '=IF(S'.$row.'<>0,D'.$row.'/S'.$row.',0)';	# calculate H P/E
		$data_hash{$key}[5] = '=IF(U'.$row.'<>0,D'.$row.'/U'.$row.',0)';	# calculate F P/E
		$data_hash{$key}[6] = '=IF(D'.$row.'<>0,Y'.$row.'/D'.$row.',0)';	# calculate Hist Yield
		$data_hash{$key}[7] = '=IF(D'.$row.'<>0,AB'.$row.'/D'.$row.',0)';	# calculate Fut Yield
		$data_hash{$key}[8] = '=IF(AC'.$row.'=AD'.$row.',0,O'.$row.'/(AC'.$row.'-AD'.$row.'))';	# calculate PTBV
		unless ($data_hash{$key}[9] =~/\d/){$data_hash{$key}[9] = ''}		# clean up Gearing
		$data_hash{$key}[10] = '=IF(S'.$row.'>0, U'.$row.'/S'.$row.' - 1,0)';	# calculate EPS growth
		$data_hash{$key}[11] = '=IF(AB'.$row.'<>0,U'.$row.'/AB'.$row.',0)';	# calculate F cover
		$data_hash{$key}[12] = '=IF(AF'.$row.'<>0,O'.$row.'/AF'.$row.',0)';	# calculate PSR
		if($data_hash{$key}[24] > $data_hash{$key}[23] and
		   $data_hash{$key}[23] > $data_hash{$key}[22] and
		   $data_hash{$key}[22] > $data_hash{$key}[21]){
			$data_hash{$key}[13] = 'Y';										# calculate Inc Div
		} else {
			$data_hash{$key}[13] = 'N'
		}
		$data_hash{$key}[14] = '=D'.$row.'*AG'.$row.'/100';					# calculate Mcap
		unless ($data_hash{$key}[17] =~/\d/){$data_hash{$key}[17] = ''}		# clean up Last Results
		unless ($data_hash{$key}[18] =~/\d/){$data_hash{$key}[18] = ''}		# clean up EPS
		#unless ($data_hash{$key}[19] =~/\d/){$data_hash{$key}[19] = ''}		# clean up FEPS
		#unless ($data_hash{$key}[20] =~/\d/){$data_hash{$key}[20] = ''}		# clean up FEPS
		unless ($data_hash{$key}[21] =~/\d/){$data_hash{$key}[21] = ''}		# clean up Div
		unless ($data_hash{$key}[22] =~/\d/){$data_hash{$key}[22] = ''}		# clean up Div
		unless ($data_hash{$key}[23] =~/\d/){$data_hash{$key}[23] = ''}		# clean up Div
		unless ($data_hash{$key}[24] =~/\d/){$data_hash{$key}[24] = ''}		# clean up Div
		unless ($data_hash{$key}[25] =~/\d/){$data_hash{$key}[25] = ''}		# clean up Div
		unless ($data_hash{$key}[26] =~/\d/){$data_hash{$key}[26] = ''}		# clean up Div
		unless ($data_hash{$key}[27] =~/\d/){$data_hash{$key}[27] = ''}		# clean up Div
		unless ($data_hash{$key}[28] =~/\d/){$data_hash{$key}[28] = ''}		# clean up Book
		unless ($data_hash{$key}[29] =~/\d/){$data_hash{$key}[29] = ''}		# clean up Intangibles
		unless ($data_hash{$key}[30] =~/\d/){$data_hash{$key}[30] = ''}		# clean up Cash
		unless ($data_hash{$key}[31] =~/\d/){$data_hash{$key}[31] = ''}		# clean up Turnover
		unless ($data_hash{$key}[32] =~/\d/){$data_hash{$key}[32] = ''}		# clean up No Shares
		$row++;
	}
	use warnings;
	
}
### end of calculated columns for excel
###########################################


###########################################
### Start of calculated columns for openoffice
sub calc_col_oo {
	my ($key, $row);
	
	$row = 2;
	no warnings;
	foreach $key (sort keys %data_hash){
		$data_hash{$key}[4] = '=IF(S'.$row.'<>0;D'.$row.'/S'.$row.';0)';	# calculate H P/E
		$data_hash{$key}[5] = '=IF(U'.$row.'<>0;D'.$row.'/U'.$row.';0)';	# calculate F P/E
		$data_hash{$key}[6] = '=IF(D'.$row.'<>0;Y'.$row.'/D'.$row.';0)';	# calculate Hist Yield
		$data_hash{$key}[7] = '=IF(D'.$row.'<>0;AB'.$row.'/D'.$row.';0)';	# calculate Fut Yield
		$data_hash{$key}[8] = '=IF(AC'.$row.'=AD'.$row.';0;O'.$row.'/(AC'.$row.'-AD'.$row.'))';	# calculate PTBV
		unless ($data_hash{$key}[9] =~/\d/){$data_hash{$key}[9] = ''}		# clean up Gearing
		$data_hash{$key}[10] = '=IF(S'.$row.'>0; U'.$row.'/S'.$row.' - 1;0)';	# calculate EPS growth
		$data_hash{$key}[11] = '=IF(AB'.$row.'<>0;U'.$row.'/AB'.$row.';0)';	# calculate F cover
		$data_hash{$key}[12] = '=IF(AF'.$row.'<>0;O'.$row.'/AF'.$row.';0)';	# calculate PSR
		if($data_hash{$key}[24] > $data_hash{$key}[23] and
		   $data_hash{$key}[23] > $data_hash{$key}[22] and
		   $data_hash{$key}[22] > $data_hash{$key}[21]){
			$data_hash{$key}[13] = 'Y';										# calculate Inc Div
		} else {
			$data_hash{$key}[13] = 'N'
		}
		$data_hash{$key}[14] = '=D'.$row.'*AG'.$row.'/100';					# calculate Mcap
		unless ($data_hash{$key}[17] =~/\d/){$data_hash{$key}[17] = ''}		# clean up Last Results
		unless ($data_hash{$key}[18] =~/\d/){$data_hash{$key}[18] = ''}		# clean up EPS
		#unless ($data_hash{$key}[19] =~/\d/){$data_hash{$key}[19] = ''}		# clean up FEPS
		#unless ($data_hash{$key}[20] =~/\d/){$data_hash{$key}[20] = ''}		# clean up FEPS
		unless ($data_hash{$key}[21] =~/\d/){$data_hash{$key}[21] = ''}		# clean up Div
		unless ($data_hash{$key}[22] =~/\d/){$data_hash{$key}[22] = ''}		# clean up Div
		unless ($data_hash{$key}[23] =~/\d/){$data_hash{$key}[23] = ''}		# clean up Div
		unless ($data_hash{$key}[24] =~/\d/){$data_hash{$key}[24] = ''}		# clean up Div
		unless ($data_hash{$key}[25] =~/\d/){$data_hash{$key}[25] = ''}		# clean up Div
		unless ($data_hash{$key}[26] =~/\d/){$data_hash{$key}[26] = ''}		# clean up Div
		unless ($data_hash{$key}[27] =~/\d/){$data_hash{$key}[27] = ''}		# clean up Div
		unless ($data_hash{$key}[28] =~/\d/){$data_hash{$key}[28] = ''}		# clean up Book
		unless ($data_hash{$key}[29] =~/\d/){$data_hash{$key}[29] = ''}		# clean up Intangibles
		unless ($data_hash{$key}[30] =~/\d/){$data_hash{$key}[30] = ''}		# clean up Cash
		unless ($data_hash{$key}[31] =~/\d/){$data_hash{$key}[31] = ''}		# clean up Turnover
		unless ($data_hash{$key}[32] =~/\d/){$data_hash{$key}[32] = ''}		# clean up No Shares
		$row++;
	}
	use warnings;
	
}
### end of calculated columns for openoffice
###########################################

###########################################
### Start of Print tab-delimited output file
sub print_output {

	my ($key, $div1, $div2, $div3, $div4, $div5, $fdiv1, $fdiv2, $f_file);
	
	($f_file) = @_;
	$div5 = $current_year;
	$div4 = $current_year-1;
	$div3 = $current_year-2;
	$div2 = $current_year-3;
	$div1 = $current_year-4;
	$fdiv1 = $current_year;
	$fdiv2 = $current_year+1;
	
	open OUTFILE, ">$f_file";
	
	#print headers
	print OUTFILE "Name\tISIN\tEPIC\tPrice\tH P/E\t$fdiv2 P/E\tH Yield\tF Yield\tPTBV\tGearing\tEPS Growth\tF Cover\t";
	print OUTFILE "PSR\tIncr Div\tCap (m)\tIndex\tSector\tLast Results\tEPS\tFeps $fdiv1\tFeps $fdiv2\t";
	print OUTFILE "Div $div1\tDiv $div2\tDiv $div3\tDiv $div4\tDiv $div5\tFdiv $fdiv1\tFdiv $fdiv2\tBook(m)\tIntangibles(m)\tCash(m)\tTurnover(m)\tNo Shares(m)\t";
	print OUTFILE "Reserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\t";
	print OUTFILE "Reserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\tReserved\n";
	
	no warnings;
	foreach $key (sort keys %data_hash){
		print OUTFILE "$data_hash{$key}[0]\t$data_hash{$key}[1]\t$data_hash{$key}[2]\t$data_hash{$key}[3]\t";
		print OUTFILE "$data_hash{$key}[4]\t$data_hash{$key}[5]\t$data_hash{$key}[6]\t$data_hash{$key}[7]\t";
		print OUTFILE "$data_hash{$key}[8]\t$data_hash{$key}[9]\t$data_hash{$key}[10]\t$data_hash{$key}[11]\t";
		print OUTFILE "$data_hash{$key}[12]\t$data_hash{$key}[13]\t$data_hash{$key}[14]\t$data_hash{$key}[15]\t";
		print OUTFILE "$data_hash{$key}[16]\t$data_hash{$key}[17]\t$data_hash{$key}[18]\t$data_hash{$key}[19]\t";
		print OUTFILE "$data_hash{$key}[20]\t$data_hash{$key}[21]\t$data_hash{$key}[22]\t$data_hash{$key}[23]\t";
		print OUTFILE "$data_hash{$key}[24]\t$data_hash{$key}[25]\t$data_hash{$key}[26]\t$data_hash{$key}[27]\t";
		print OUTFILE "$data_hash{$key}[28]\t$data_hash{$key}[29]\t$data_hash{$key}[30]\t$data_hash{$key}[31]\t$data_hash{$key}[32]\n";
	}
	use warnings;
	close OUTFILE;
	
	
	
}
### end of Start of Print tab-delimited output file
###########################################

###########################################
### start of convert date
### converts dd-mmm-yy to yyyymmdd
sub convert_date {
	my ($date, %month_hash, $day, $month, $year, $yyyymmdd);
	($date) = @_;

	%month_hash = ('Jan'=>'01', 'Feb'=>'02', 'Mar'=>'03', 'Apr'=>'04', 'May'=>'05', 'Jun'=>'06',
				   'Jul'=>'07', 'Aug'=>'08', 'Sep'=>'09', 'Oct'=>'10', 'Nov'=>'11', 'Dec'=>'12');
	
	$date =~/(\d\d)-(\w\w\w)-(\d\d)/;
	$day   = $1;
	$month = $2;
	$year  = $3;
	$month = $month_hash{$month};
	
	$yyyymmdd = '20'.$year.$month.$day;
	return $yyyymmdd;
	
}
### end of convert date
###########################################

##########################################
### Start of dump_data_hash to write data_hash to a file

sub dump_data_hash {
	
	no warnings;
	open OUTFILE, ">$f_hashdump";		
		
		foreach $key (sort keys %data_hash){
			print OUTFILE "$data_hash{$key}[0]\t$data_hash{$key}[1]\t$data_hash{$key}[2]\t$data_hash{$key}[3]\t";
			print OUTFILE "$data_hash{$key}[4]\t$data_hash{$key}[5]\t$data_hash{$key}[6]\t$data_hash{$key}[7]\t";
			print OUTFILE "$data_hash{$key}[8]\t$data_hash{$key}[9]\t$data_hash{$key}[10]\t$data_hash{$key}[11]\t";
			print OUTFILE "$data_hash{$key}[12]\t$data_hash{$key}[13]\t$data_hash{$key}[14]\t$data_hash{$key}[15]\t";
			print OUTFILE "$data_hash{$key}[16]\t$data_hash{$key}[17]\t$data_hash{$key}[18]\t$data_hash{$key}[19]\t";
			print OUTFILE "$data_hash{$key}[20]\t$data_hash{$key}[21]\t$data_hash{$key}[22]\t$data_hash{$key}[23]\t";
			print OUTFILE "$data_hash{$key}[24]\t$data_hash{$key}[25]\t$data_hash{$key}[26]\t$data_hash{$key}[27]\t";
			print OUTFILE "$data_hash{$key}[28]\t$data_hash{$key}[29]\t$data_hash{$key}[30]\t$data_hash{$key}[31]\t$data_hash{$key}[32]\n";
		}

	close OUTFILE;
	use warnings;
}

### end of dump_data_hash to write data_hash to a file
##########################################


##########################################
### Start of read_data_hash to read data_hash from a file

sub read_data_hash {
	
	my ($record, @datahash_array);
	
	open INFILE, $f_hashdump;
	while (<INFILE>){
		$record = $_;
		chomp($record);
		@datahash_array = split("\t", $record);
		$data_hash{$datahash_array[2]}=[@datahash_array];
	}
	close INFILE;
	
}

### end of read_data_hash to read data_hash from a file
##########################################
