I need to make a stock ticker in the Perl Language here is the code that I have,
ID: 669368 • Letter: I
Question
I need to make a stock ticker in the Perl Language here is the code that I have, but I will not wrok. Can someone, ook at it, adn see where my problem is? code is below...
#!/usr/bin/perl -w
#
use strict;
use Finance::Quote;
# Set preferred currency
my $CURRENCY = "CNY";
# Assign file name to scalar variable, read file, and then assign to an array
my $techticker = "techticker.txt";
open (FH, "< $techticker") or die $!;
my @STOCKS = <FH>;
print @STOCKS, " ";
close FH or die "Cannot close $techticker: $!";
# Define the format : label, style formatting, width of the field
my @labels = (["name", "%24s", 15],
["date", "%11s", 17],
["time", "%8s", 10],
["last", "%8.2f", 8],
["high", "%8.2f", 8],
["low", "%8.2f", 8],
["close", "%8.2f", 8],
["volume","%10d", 10]);
# Seconds between refresh
my $REFRESH = 120;
# --- END CONFIG SECTION ---
my $quoter = Finance::Quote->new();
my $clear = `clear`; # So we can clear the screen.
# Build our header.
my $header = " MEGA CAPITAL TECHNOLOGY COMPANY STOCK REPORT" .($CURRENCY ? " ($CURRENCY)" : "") ." ";
foreach my $tuple (@labels) {
my ($name, undef, $width) = @$tuple;
$header .= sprintf("%".$width."s",uc($name));
}
$header .= " ".("-"x85)." ";
# --- END HEADER SECTION ---
# Set default currency.
$quoter->set_currency($CURRENCY) if $CURRENCY;
for (;;) { # For ever.
print $clear,$header;
foreach my $stockset (@STOCKS) {
my ($exchange, @symbols) = $stockset; # stuck in here!!! /// <<<______-----------------------//
my %info = $quoter->fetch($exchange,@symbols);
foreach my $symbol (@symbols) {
next unless $info{$symbol,"success"}; # Skip failures.
foreach my $tuple (@labels) {
my ($label,$format) = @$tuple;
printf $format,$info{$symbol,$label};
}
print " ";
}
}
sleep($REFRESH);
}
Explanation / Answer
#!/usr/bin/perl -w
#
use strict;
use Finance::Quote;
# Set preferred currency
my $CURRENCY = "CNY";
# Assign file name to scalar variable, read file, and then assign to an array
my $techticker = "techticker.txt";
open (FH, "< $techticker") or die $!;
my @STOCKS = <FH>;
print @STOCKS, " ";
close FH or die "Cannot close $techticker: $!";
# Define the format : label, style formatting, width of the field
my @labels = (["name", "%24s", 15],
["date", "%11s", 17],
["time", "%8s", 10],
["last", "%8.2f", 8],
["high", "%8.2f", 8],
["low", "%8.2f", 8],
["close", "%8.2f", 8],
["volume","%10d", 10]);
# Seconds between refresh
my $REFRESH = 120;
# --- END CONFIG SECTION ---
my $quoter = Finance::Quote->new();
my $clear = `clear`; # So we can clear the screen.
# Build our header.
my $header = " MEGA CAPITAL TECHNOLOGY COMPANY STOCK REPORT" .($CURRENCY ? " ($CURRENCY)" : "") ." ";
foreach my $tuple (@labels) {
my ($name, undef, $width) = @$tuple;
$header .= sprintf("%".$width."s",uc($name));
}
$header .= " ".("-"x85)." ";
# --- END HEADER SECTION ---
# Set default currency.
$quoter->set_currency($CURRENCY) if $CURRENCY;
for (;;) { # For ever.
print $clear,$header;
foreach my $stockset (@STOCKS) {
my ($exchange, @symbols) = $stockset; # stuck in here!!! /// <<<______-----------------------//
my %info = $quoter->fetch($exchange,@symbols);
foreach my $symbol (@symbols) {
next unless $info{$symbol,"success"}; # Skip failures.
foreach my $tuple (@labels) {
my ($label,$format) = @$tuple;
printf $format,$info{$symbol,$label};
}
print " ";
}
}
sleep($REFRESH);
}