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:
Can I return a hash of hashes?

 



PapaGeek
User

Aug 14, 2013, 4:59 AM

Post #1 of 3 (508 views)
Can I return a hash of hashes? Can't Post

Im working on a package to read financial web pages and return information.

Here is an example of the kind of data Id like to return for an available stock symbols:


Code
use Modern::Perl '2013'; 

main();

sub main
{
my %hashStocks = getStockData();
for my $Stock (sort keys %hashStocks)
{
my $bid = $hashStocks{$Stock}->{'bid'};
my $ask = $hashStocks{$Stock}->{'ask'};
print "$Stock: bid -> $bid ask -> $ask\n";
}
}

sub getStockData
{
my %hashStocks;
my %thisStock;
$thisStock{'bid'} = 25;
$thisStock{'ask'} = 30;
$hashStocks{'C'} = %thisStock;
$thisStock{'bid'} = 35;
$thisStock{'ask'} = 40;
$hashStocks{'DELL'} = %thisStock;
}


I get all kind of errors, so this is obviously not the way to do this.

What is the proper way to return a list of stock symbols where each symbol includes multiple pieces of information about that symbol?


FishMonger
Veteran / Moderator

Aug 14, 2013, 6:20 AM

Post #2 of 3 (505 views)
Re: [PapaGeek] Can I return a hash of hashes? [In reply to] Can't Post


Code
sub getStockData  
{
my %thisStock;
$thisStock{'C'}{'bid'} = 25;
$thisStock{'C'}{'ask'} = 30;
$thisStock{'DELL'}{'bid'} = 35;
$thisStock{'DELL'}{'ask'} = 40;

return %thisStock;
}


or

Code
sub getStockData  
{
my %thisStock;
$thisStock{'C'} = {'bid' => 25, 'ask' => 30};
$thisStock{'DELL'} = {'bid' => 35, 'ask' => 40};

return %thisStock;
}


or

Code
sub getStockData  
{
my %thisStock = (
'C' => {'bid' => 25, 'ask' => 30},
'DELL' => {'bid' => 35, 'ask' => 40}
);

return %thisStock;
}



PapaGeek
User

Aug 14, 2013, 6:50 AM

Post #3 of 3 (503 views)
Re: [FishMonger] Can I return a hash of hashes? [In reply to] Can't Post

Thank you, the first example will work best for the code I have to write:

Code
 sub getStockData 
{
my %stockData;
my $stock = 'C';
$stockData{$stock}{'bid'} = 25;
$stockData{$stock}{'ask'} = 30;
$stock = 'DELL';
$stockData{$stock}{'bid'} = 35;
$stockData{$stock}{'ask'} = 40;
return %stockData;
}


It will allow me to examine each table line of the returned web page element by element, then place the data in each line as interpreted. The variable key will be the stock symbol itself as shown in the example.

If anyone would like a copy of the package that I am writing, just ask and I will post it. It is designed to obtain stock market information from web pages.

 
 


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

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