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:
Printing Keys of hash as header

 



Amrit
Novice

Dec 20, 2011, 8:43 AM

Post #1 of 2 (646 views)
Printing Keys of hash as header Can't Post

Hi,

I am using a new language "Q" to extract data from DB.

I want to print data returned from Q language.I am getting this data in hash.

The data returned is in format


price_scale
1
1
1
1
vd
1315526400
1315526400
1315526400
1315526400
id
AAPL
AAPL
AAPL
AAPL

I want to print the returned hash in format


price_scale,vd,id
1,1315526400,AAPL
1,1315526400,AAPL
1,1315526400,AAPL
1,1315526400,AAPL


Can somebody please help me.It is urgent.

Thanks in advance.



PS :

foreach (keys %$result1)
{
foreach (@{$result1->{$_}})
{
print $_.",";
}
print "\n";
}


My try gives me only this:

1,1,1,1,
1315526400,1315785600,1315872000,1315958400,
AAPL.O,AAPL.O,AAPL.O,AAPL.O,


BillKSmith
Veteran

Dec 20, 2011, 10:43 AM

Post #2 of 2 (639 views)
Re: [Amrit] Printing Keys of hash as header [In reply to] Can't Post

$result1 is a reference to a hash of arrays (which is very similar to a two dimensional array).


Code
  

use strict;
use warnings;
my $result1 = {
price_scale => [ (1) x 4 ],
vd => [ (1315526400) x 4 ],
id => [ ('AAPL') x 4 ],
};
foreach ( keys %$result1 ) {
foreach ( @{ $result1->{$_} } ) {
print $_. ",";
}
print "\n";
}
print "\n\n\n";

print join( ',', keys %$result1), "\n"; # Header
foreach my $i (0..3) {
foreach my $key ( keys %$result1 ) {
print $result1->{$key}->[$i], ','; # Data
}
print "\n";
}

Good Luck,
Bill

 
 


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

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