"Daniel Falkenberg" <[EMAIL PROTECTED]> wrote:
> Hello again,
Hi.
*Always* put the following at the top of your code:
use strict; # requires declaration of all variables
use warnings; # gives warnings on mistakes and suggestions
These two lines will save you countless hours of debugging silly mistakes.
> foreach $stock (@stocks) {
>
> my $html_string =
> get("http://www.asx.com.au/asx/markets/PriceResults.jsp?method=get&templ
> ate=F1001&ASXCodes=$stock");
>
> $te = new HTML::TableExtract( headers => [qw(Code Last \$)] );
> $te->parse($html_string);
> # Examine all matching tables
> foreach $ts ($te->table_states) {
> # print "Table (", join(',', $ts->coords), "):\n";
> foreach $row ($ts->rows) {
> join (',', @$row);
What is this join doing here? Presently it's in void context, meaning you
are doing absolutely nothing with it.
> @downloaded_stocks = @$row;
This line is just fine but why not just use $row directly?
$row->[0] equals $downloaded_stocks[0]
$row->[1] equals $downloaded_stocks[1]
$row->[2] equals $downloaded_stocks[2]
> }
> %stock_hash = (
> $downloaded_stocks[0] => {
> "Trading Price" => "\$$downloaded_stocks[1]",
> "Price Change" => "\$$downloaded_stocks[2]",
> },
> );
There are two problems here. One, you must declare %stock_hash before your
loop so that it is still available to your program after the loop. Two, you
are setting the entire %stock_hash each time through the loop, which means
it will contain only the last stock you processed within the loop. What you
intended was to insert a key for each stock and its value will contain an
anonymous hash.
I think this is what you intended:
--------BEGIN CODE--------
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use HTML::TableExtract;
my @stocks = qw(ABS AGZ);
my %stock_hash;
foreach my $stock (@stocks) {
my $html_string =
get("http://www.asx.com.au/asx/markets/PriceResults.jsp?method=get&template=
F1001&ASXCodes=$stock");
my $te = new HTML::TableExtract( headers => [ qw(Code Last \$) ] );
$te->parse($html_string);
# Examine all matching tables
foreach my $ts ($te->table_states) {
foreach my $row ($ts->rows) {
my @downloaded_stocks = @$row;
$stock_hash{ $downloaded_stocks[0] } =
{ "Trading Price" => "\$$downloaded_stocks[1]",
"Price Change" => "\$$downloaded_stocks[2]",
};
}
}
}
for my $code ( sort keys %stock_hash ) {
print "$code: ";
for my $details ( sort keys %{ $stock_hash{$code} } ) {
print "$details=$stock_hash{$code}{$details} ";
}
print "\n";
}
---------END CODE---------
Good luck,
ZO
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>