use LWP::Simple;
#USe DBI;
#my $name = <stdin>;

@names = ("GOOG","MSFT","MO","AAPL","AIG", "BSC", "AMZN", "AMD", "T", "ADBE", "ANF", "GS","BKS", "HPQ","MER", "LMT", "HON", "TWX", "GT", "NYT", "TXN", "NATI", "QCOM", "LEH", "VZ", "VOD", "S", "IBM",  "YHOO", "DIS", "WMT", "WAG", "SI", "BLK", "XLNX", "ADI", "BAC", "INTC", "WB", "LHL.F" ,"DELL", "TTM", "INFY", "TCS.NS", "CTSH", "ACN", "CSCO", "DFG", "JPM", "SNE", "PHG", "JNJ","SLB", "C", "DB", "BSC", "UBS");

#print $#names;

$str = get "http://finance.yahoo.com/q/hp?s=^GSPC";

@array = split(/\<tr\>/,$str);

#$dsn = "DBI:mysql:database=group802;";

#$dbh = DBI->connect($dsn, "group802", "Cor\@538",{AutoCommit => 0});

#$dbh->do("use group802");
$zz = 0;

$a = 0;
@snp = ();
foreach(@array) {
	@vals = ();
    @data =  split(/\<td /,$_);

    foreach my $line (@data)
    {
        if($line =~ /(.*)tabledata1(.*)\>(.*)\<\/td\>/)
        {
            $a = 1;
            $v = $3;

            if($v =~ /,/)
            {
                $v =~ s/,//g;
            }

	    push(@vals,$v);
            $query .= ",\"$v\"";
        }
    }

    if($a != 1) {
	next;
    }
    push(@snp,$vals[4]); 
}

$hi = "";
foreach $name (@names) {
chomp $name;
++$z;
$str = get "http://finance.yahoo.com/q/hp?s=".$name;

@array = split(/\<tr\>/,$str);


$c = 0;
$a = 0;
@vals = ();
foreach(@array)
{
#    $query = "INSERT into stock_table values(\"$name\"";
#    $query = "";
	@vals = ();
    @data =  split(/\<td /,$_);

    foreach my $line (@data)
    {
        if($line =~ /(.*)tabledata1(.*)\>(.*)\<\/td\>/)
        {
            $a = 1;
            $v = $3;

            if($v =~ /,/)
            {
                $v =~ s/,//g;
            }

	    push(@vals,$v);
            $query .= ",\"$v\"";
        }
    }

   if($a != 1 || $vals[5] == 0) {
	next;
   }
     
    $query .= ");";

  # # $dbh->do($query) unless ($a == 0);
        print $query;
    $query = "";
    print $vals[4]." ";
    $mfi = $vals[2] - $vals[3];
    $mfi = $mfi/$vals[5];

    print sprintf("%.15f",$mfi)." ".$snp[$c];
   $c++; 
    print "\n" unless ($a == 0);
   $a = 0;
}

#print $name." $z ".$c."\n";
}


