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: Re: [hwnd] Reading results back that were dumped?: Edit Log



Chris Charley
User

Apr 10, 2013, 9:45 AM


Views: 1612
Re: [hwnd] Reading results back that were dumped?


Code
print "$_, -> ${$rows{$_}}[0], -> ${$rows{$_}}[1]\n";    

print "$_, -> $rows{$_}[0], -> $rows{$_}[1]\n";


The second form is the correct one, I believe. And the suggestion by FishMonger to use selectall_hashref may be applicable to your case.
Update: or maybe (unsure how the code reads

Code
print "$_, -> $rows{$_}->[0], -> $rows{$_}->[1]\n";



Here is a sample use in a small program, (followed by the results of the run).

Code
 #!/usr/bin/perl  
use strict;
use warnings;
use 5.014;
use DBI;
use Data::Dumper;

my $dbh = DBI->connect(qq{DBI:CSV:});
$dbh->{'csv_tables'}->{'data'} = { 'file' => 'o33.txt',
'csv_sep_char' => ' '};

my $statement = (qq{
SELECT lastname, firstname, age, gender, phone
FROM data
});

my $key_field = 'lastname';
my $href = $dbh->selectall_hashref($statement, $key_field);
$dbh->disconnect();

print Dumper $href;

__END__
contents o33.txt

lastname firstname age gender phone
mcgee bobby 27 M 555-555-5555
kincaid marl 67 M 555-666-6666
hofhazards duke 22 M 555-696-6969


Code
   
C:\Old_Data\perlp>perl t7.pl
$VAR1 = {
'hofhazards' => {
'firstname' => 'duke',
'lastname' => 'hofhazards',
'phone' => '555-696-6969',
'age' => '22',
'gender' => 'M'
},
'mcgee' => {
'firstname' => 'bobby',
'lastname' => 'mcgee',
'phone' => '555-555-5555',
'age' => '27',
'gender' => 'M'
},
'kincaid' => {
'firstname' => 'marl',
'lastname' => 'kincaid',
'phone' => '555-666-6666',
'age' => '67',
'gender' => 'M'
}
};

C:\Old_Data\perlp>

An observation - in your sql statement, you have an ORDER BY clause, but I think that may not be necessary because, the order of newsid gets lost once inserted to the hash.


(This post was edited by Chris Charley on May 1, 2013, 5:22 PM)


Edit Log:
Post edited by Chris Charley (User) on May 1, 2013, 5:22 PM


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

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