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: Beginner:
How to print 2D hash?

 



bulrush
Novice

Jul 11, 2014, 11:23 AM

Post #1 of 6 (586 views)
How to print 2D hash? Can't Post

I just installed Activestate Perl 5.14 on my Win8 machine, and then used PPM to install Finance::Quote.

I'm new to 2D hashes. I wrote a program to get a stock quote, here it is.


Code
$qsrv=Finance::Quote->new; 
$qsrv->timeout(60);
$qsrv->set_currency("USD");
$qsrv->require_labels(qw/date name close/);
$qsrv->failover(1);

eval {
%quotes=$qsrv->fetch("nyse",$sym); # Returns 2 dimensional hash.
};
if ($@)
{
print "ERROR in fetch: $@\n";
exit;
}

@q=keys(%quotes);
print Dumper(\%quotes);
#print "The price of $sym is ".$quotes{"dlph","close"};
print "The price of $sym is ".$quotes{"DLPH"}{"close"}; # ERROR here


However, when I print $quotes{"DLPH"}{"close"} I get "Uninitialized value in concatenation or string. I'm not sure what is wrong with it.

I also double checked my 2D hash syntax on the web and that seems correct. Using Data::Dumper there is stuff in the hash after ->fetch(), but I can't get it out. Data::Dumper output. Keep in mind this was copied to the clipboard from a Win8 cmd window.


Code
$VAR1 = { 
'DLPH∟ask' => undef,
'DLPH∟avg_vol' => '2038630',
'DLPH∟bid' => undef,
'DLPH∟cap' => '21153000000',
'DLPH∟close' => '69.47',
'DLPH∟currency' => 'USD',
'DLPH∟date' => '07/11/2014',
'DLPH∟day_range' => '69.34 - 69.91',
'DLPH∟div' => '0.59',
'DLPH∟div_date' => 'May 28',
'DLPH∟div_yield' => '0.85',
'DLPH∟eps' => '4.051',
'DLPH∟ex_div' => 'Feb 13',
'DLPH∟high' => '69.91',
'DLPH∟isodate' => '2014-07-11',
'DLPH∟last' => '69.66',
'DLPH∟low' => '69.34',
'DLPH∟name' => 'Delphi Automotive',
'DLPH∟net' => '+0.19',
'DLPH∟open' => '69.34',
'DLPH∟p_change' => '+0.27',
'DLPH∟pe' => '17.15',
'DLPH∟price' => '69.66',
'DLPH∟success' => 1,
'DLPH∟symbol' => 'DLPH',
'DLPH∟time' => '14:02',
'DLPH∟volume' => '539821',
'DLPH∟year_range' => '53.07 - 71.27'
};


Any ideas what's wrong here?

Thanks.

p.s. Finance::Quote 1.18 is what comes with AS Perl 5.16, and F::Q 1.18 doesn't work. So I installed AS Perl 5.14, which, oddly enough, installed the lastest F::Q 1.35.
-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

(This post was edited by bulrush on Jul 11, 2014, 1:00 PM)


Laurent_R
Veteran / Moderator

Jul 11, 2014, 11:49 AM

Post #2 of 6 (564 views)
Re: [bulrush] How to print 2D hash? [In reply to] Can't Post

The Data::Dumper shows that, obviously, the data in the hash does not look as what you expect. I do not know the reasons (I do not know this module), but did you try to print:


Code
print "The price of $sym is " . $quotes{'DLPH∟close'} . "\n";



bulrush
Novice

Jul 11, 2014, 12:41 PM

Post #3 of 6 (521 views)
Re: [Laurent_R] How to print 2D hash? [In reply to] Can't Post

Updated original post to have a link to Finance::Quote. You can read what is supposed to be returned in different contexts. Whole program is below.


Code
#c:\perl64\bin\perl 
# July 2014
# For Windows 8 64-bit.
# Get and track stock quotes, mainly last closing price
# using Finance::Stock module.
use warnings;

my($s);
use Data::Dumper; # print Dumper(%hash)
$Data::Dumper::Indent=1;
$Data::Dumper::Sortkeys=1;

$s="Finance::Quote";
eval("use $s;");
if ($@)
{
die "ERROR: $s not installed.\n";
}
$s="Text::Wrap";
eval("use $s;");
if ($@)
{
die "ERROR: $s not installed.\n";
}
$columns=70; # For Text::Wrap
$columns=70;

#############################################################
# Declare variables. Use 'our' for global to external pl files.
my($VER,$SEP);
my(@a,@b); # temp variables
my($i,$j,$t);
my($qsrv,$sym);
my(@quotearr,@stocks,%qhash);

$SEP=chr(9);
$VER='2014-0711a';

#############################################################
# Check args
print "\n"; # print blank line to separate warning messages from our messages.
if (!(defined($ARGV[0])))
{print "$VER Usage: perl $0 ok\n";
$s= "\n";
print wrap('','',$s);
print "Current Perl version: $]\n";
print "\n";
exit;
}
print "Finance::Quote version: $Finance::Quote::VERSION\n";

##########
# Begin main
@q=();
%qhash=();

$sym='dlph';
@stocks=("dlph");
print "Getting quotes...\n";
$qsrv=Finance::Quote->new;
$qsrv->timeout(60);
$qsrv->set_currency("USD");
$qsrv->require_labels(qw/date name close/);
$qsrv->failover(1);

eval {
%qhash=$qsrv->fetch("nyse",$sym); # Returns 2 dimensional hash.
};
if ($@)
{
print "ERROR in fetch: $@\n";
exit;
}
#@q=$qsrv->fetch("usa",("dph"));
@q=keys(%qhash);
print Dumper(\%qhash);
print "The price of $sym is ".$qhash{"DLPH"."∟"."close"};
#print "The close price of $sym is ".$qhash{$sym}{close};

#print Dumper(\@q);

exit; # Main pgm.
#############################################################
#############################################################
#############################################################
sub writeerr
{my($s)=@_;

print "$s\n";

return;
}
#############################################################
#############################################################
#############################################################
#############################################################

-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

(This post was edited by bulrush on Jul 11, 2014, 12:50 PM)


FishMonger
Veteran / Moderator

Jul 11, 2014, 12:51 PM

Post #4 of 6 (512 views)
Re: [bulrush] How to print 2D hash? [In reply to] Can't Post

The module's documentation is very clear on this point. The fetch() method returns a hash or reference to a hash depending on the calling context. It DOES NOT return a HoH (Hash of Hashes) as you thought it did.


bulrush
Novice

Jul 11, 2014, 1:01 PM

Post #5 of 6 (501 views)
Re: [FishMonger] How to print 2D hash? [In reply to] Can't Post

The debug dumper output was copied from a Win8 cmd window, if that matters.

I can't seem to get any data out of this. I thought a hash ref would be like this: $href->{DLPH∟close"}

Can someone help me out? How do I print the value of the DLPH close?

I did find this in the docs: "With the exception of straight currency exchange rates, all information is returned as a two-dimensional hash (or a reference to such a hash, if called in a scalar context). "
-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

(This post was edited by bulrush on Jul 11, 2014, 1:04 PM)


bulrush
Novice

Jul 11, 2014, 1:08 PM

Post #6 of 6 (494 views)
Re: [bulrush] How to print 2D hash? [In reply to] Can't Post

Got it.


Code
my($href); 
eval {
$href=$qsrv->fetch("nyse",$sym); # Returns 2 dimensional hash.
};
if ($@)
{
print "ERROR in fetch: $@\n";
exit;
}

print "The close price of $sym is ".$href->{uc($sym),"close"}."\n";

-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

 
 


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

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