CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
NASDAQ Stock - Print File VS Console problem

 



wolfe12437
Novice

Mar 10, 2011, 11:02 PM

Post #1 of 7 (2819 views)
NASDAQ Stock - Print File VS Console problem Can't Post

Ok, maybe I'm a newbie and missing the obvious. I've been at this for hours and can't see what I am missing. The print to console works as expected. The stock symbol is correct but when I use the same print statement to print to the file, the old string text appears. Can someone advise. Here's the code


Code
 #!/usr/bin/perl 

use strict;
use HTML::Tree;
use LWP::Simple;

my $stock;
my $symbol;
my $filler1;
my $filler2,
my $lprice;
my $change_pct;
my $change;
my $pct;
my $vol;

my $url = 'http://www.nasdaq.com/extended-trading/premarket-mostactive.aspx';
my $page = get($url) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );
my @links = $p->look_down( _tag => 'tr', );
open(FILE, ">stockstrings.txt");
for my $row (@links) {
my @cells = $row->look_down( _tag => 'td' );
print FILE join( "\t", map { $_->as_trimmed_text( ) } @cells ) . "\n";
}
$p = $p->delete; # don't need it anymore

close(FILE);
open (FILE, 'stockstrings.txt');

while (<FILE>) {
chomp;
($stock,$filler1, $filler2, $lprice,$change_pct,$vol) = split("\t");
if ($stock =~/(.*?)Real-time QuoteStock NewsStock ChartPre-Market/) { $symbol =$1; }
if ($change_pct =~/(.*?)\xA0\xA0/) { $change =$1; }
if ($change_pct =~/\xA0\xA0(.*?)[\%]/) { $pct =$1; }

print "$symbol\t$lprice\t$change\t$pct\t$vol\n";
print FILE "$symbol\t$lprice\t$change\t$pct\t$vol\n";
}
close (FILE);
#exit;



Karazam
User

Mar 10, 2011, 11:50 PM

Post #2 of 7 (2817 views)
Re: [wolfe12437] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

You try to write to the same file as you read from in the while loop.


(This post was edited by Karazam on Mar 10, 2011, 11:52 PM)


FishMonger
Veteran / Moderator

Mar 11, 2011, 5:50 AM

Post #3 of 7 (2810 views)
Re: [wolfe12437] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

Why are you needlessly writing the data out to a file and then immediately reopen the file to read-in and process that same data?


wolfe12437
Novice

Mar 11, 2011, 2:50 PM

Post #4 of 7 (2793 views)
Re: [Karazam] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

I tried substituting different file names for FILE (and different file handles) but that did not solve the problem. Did you try it and it worked? If so, can you post your code?


(This post was edited by wolfe12437 on Mar 11, 2011, 2:51 PM)


wolfe12437
Novice

Mar 11, 2011, 3:07 PM

Post #5 of 7 (2791 views)
Re: [FishMonger] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

Ok Fish Monger.

You're right I'm a newbie trying to get by as an intermediate. I tried to figure out the calling syntax for HTML::Treebuilder and HTML::Element. However, whatever I tried to get the data it wanted, I got undefined or, what I think was the reference to a Hash.... so instead of getting the text contents of the cell on the webpage table, I got a reference like HTML::Element:ARRAY(0x83c38) and, am sorry to say, I could not figure out how to get to the text properly.

So, like any other newbie hack, I stole code snippets off the web that let me dump the text I wanted to a flat file and then read the file back in for the next step in process. How's that for groveling - grin. Appreciate any and all advice.


Karazam
User

Mar 12, 2011, 2:06 AM

Post #6 of 7 (2774 views)
Re: [wolfe12437] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

You almost got it already. It's as simple as


Code
open (FILE, 'stockstrings.txt');  # open for reading 
open (RESULT, '>result.txt'); # open for writing

while (<FILE>) {
...
print RESULT "$symbol\t$lprice\t$change\t$pct\t$vol\n";
}


Let me just add that that syntax for filehandles are considered outdated. The preferred way nowadays is to use lexical filehandles, like so:


Code
open my $file, '<', 'stockstrings.txt';  # open for reading 
open my $result, '>', 'result.txt'; # open for writing

while (<$file>) {
...
print $result ...
}


But you often see the old way, and in most cases it works fine.
Hope this helps. Smile


wolfe12437
Novice

Mar 12, 2011, 10:31 AM

Post #7 of 7 (2761 views)
Re: [Karazam] NASDAQ Stock - Print File VS Console problem [In reply to] Can't Post

Thanks a lot Karazam. Here's my final code and runs great. Probably still inelegant and inefficient. I needed this fixed because I had some old code that broke when the NASDAQ webpage changed. Had a buddy who wrote the old broken Perl code but this new Perl code I can understand and can fix myself (with helpful advice from this Perl forum). Thanks again.



Code
 #!/usr/bin/perl 

# script reads Nasdaq pre-open page, parses advance and declined tab for stock data and puts in appropriate file for further processing
use strict;
use HTML::Tree;
use LWP::Simple;

my $symbol;
my $lastpr;
my $openpr;
my $pct;
my $vol;

my $count;
my $stock;
my $filler1;
my $filler2,
my $change_pct;
my $change;

#read webpage using Treebuilder and put write stock fields to flat file
my $url = 'http://www.nasdaq.com/extended-trading/premarket-mostactive.aspx';
my $page = get($url) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );
my @links = $p->look_down( _tag => 'tr', );

open my $file, '<', 'stockstrings.txt'; # open for reading
for my $row (@links) {
my @cells = $row->look_down( _tag => 'td' ); # look at HTML Table tags and put cell strings in an array
print $file join( "\t", map { $_->as_trimmed_text( ) } @cells ) . "\n";
}
$p = $p->delete; # don't need it anymore
close $file;

#cleanup stock data. Drop un-needed rows (blank and most active stocks), calculate Opening price for both advancing and declining stocks.
#Put $ sign and % sign back in
open my $file, '<', 'stockstrings.txt'; # open for reading
open my $result, '>', 'stocks.txt'; # open for writing
$count = 1; # set while counter
while (<$file>) {
chomp;
($stock,$filler1, $filler2, $lastpr,$change_pct,$vol) = split("\t");
if ($stock =~/(.*?)Real-time QuoteStock NewsStock ChartPre-Market/) { $symbol =$1; }
else
{ next;
}
$lastpr = substr($lastpr, 2); #get rid of leading "$ "
if ($change_pct =~/(.*?)\xA0\xA0/) { $change =$1; } #parse out the non printing characters between change and pct
if ($change_pct =~/\xA0\xA0(.*?)[\%]/) { $pct =$1; } #parse out the non printing characters between change and pct
if ($count <= 10) { $count ++; #increment counter so we can skip the most active 10 stocks
next;
}
if (($count >= 11) && ($count <= 20)){
$openpr = $lastpr+$change;
print $result "$symbol\t\$$lastpr\t\$$openpr\t$pct\%\t$vol\n";
}
else {
$openpr = $lastpr-$change;
print $result "$symbol\t\$$lastpr\t\$$openpr\t$pct\%\t$vol\n";
}
$count ++; #increment if reading a line
}
close $file;
close $result;

# break out advancing from declining and put in separate files for later processing
open my $file, '<', 'stocks.txt'; # open for reading
open my $upfile, '>', 'outup.txt'; # open for writing
open my $dnfile, '>', 'outdn.txt'; # open for writing

$count = 1; # set while counter
while (<$file>) {
chomp;
if ($count <= 10) {
print $upfile "$_\n";
}
else {
print $dnfile "$_\n";
}
$count ++; #increment if printed a row
}

close $file;
close $upfile;
close $dnfile;

exit;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives