Finance::Quote; and foreach
mtaivalmaa
created: 2006-04-02 23:30:39

I can't get the $ans to return any value when used in
the $myhash{$ans,'price'}; statement.  
The first part print $ans . "-" works fine. 

Why wont the $ans variable work in the function?

I have tried a static variable and it works fine.

Thanks Mike

************************************************

#!/usr/bin/perl

use Finance::Quote;

open (SLIST,";
close (SLIST);

$q = Finance::Quote->new;
$cc = 'usa';
$symb = 'WTVN.PK';

%myhash = $q->fetch($cc,@stocks);

foreach $ans (@stocks){

print $ans ."-". $myhash{$ans,'price'};

}
Re: Finance::Quote; and foreach
created: 2006-04-03 01:48:08
[chomp] your input data:
chomp(@stocks = );
Finance::Quote returns the data even though you have the linefeeds, but your for loop does not access the data because the hash keys do not have linefeeds.
Re^2: Finance::Quote; and foreach
created: 2006-04-03 09:53:53

chomp ROCKS!!  

Thanks for the help it worked great!

Re: Finance::Quote; and foreach
created: 2006-04-03 02:02:43
Your code worked for me. I used it to look up Google's stock, and got a correct value of 390.

I think your problem lies in your file - for some reason, perl isn't reading in the values from the file as you expect it would. Since you (and I) hard code a variable, and it works fine, plus the fact that I don't know what your /scripts/test.txt file looks like, I suspect it's something with that file.

Here's the code I got to work, with comments. I included some code you can run to help confirm if it's reading in the contents of the file correctly.

#!/usr/bin/perl

use Finance::Quote;
use Carp;

# This should give you more info if it couldn't read the file
open SLIST, <, "/scripts/test.txt"
        or croak "Cannot open file - $!\n";
@stocks = ;
close (SLIST);

# This will confirm that the stocks read in from the file are 
#  what you expect
print "Checking stocks: ";
for my $stock (@stocks) {
        print "$stock ";
}
print "\n";

# the following code (uncommented) worked fine for me. 
#   It got google's stock quote correctly
#@stocks = ("GOOG");

$q = Finance::Quote->new;
$cc = 'usa';
$symb = 'WTVN.PK';  # What is this used for?  If not needed, good to delete it

%myhash = $q->fetch($cc,@stocks);

foreach $ans (@stocks){
        print $ans ."-". $myhash{$ans,'price'};
}

-- Burvil

Re^2: Finance::Quote; and foreach
created: 2006-04-03 12:29:05
If you actually read the data from a file instead of hardcoding it, you would see the problem.

perlmonks.org content © perlmonks.org and bowei_99, mtaivalmaa, runrig

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03