Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Storing data into hashes

by nithins (Sexton)
on Dec 31, 2012 at 06:50 UTC ( #1011007=note: print w/ replies, xml ) Need Help??


in reply to Storing data into hashes

assuming you know to use DBI module, appending a sample example to do your work

my %has; my $query ="select * from table name "; my $exe = $dbh->prepare($query); $exe->execute(); while(my $data = $exe->fetchrow_hashref){ $has{$data->{firstattribute}}=$data->{secondattribute}; }


Comment on Re: Storing data into hashes
Download Code
Re^2: Storing data into hashes
by rkrish (Acolyte) on Dec 31, 2012 at 09:55 UTC

    Hi, I have tried this but unable to see any data stored in hash. The query is valid and it is fetching the data.I have verified it.

    sub loadSeqNbr { my %SeqRec; my $SeqRecord = $lda->prepare("select seq_nbr,data_record from usa +ge_inq" ); $SeqRecord->execute() while ( my $data = $SeqRecord->fetchrow_hashref()) { $SeqRec{$data->{seq_nbr}}=$data->{data_record}; } print ("content of hash is %SeqRec \n"); }
    output is:
    content of hash is %SeqRec

      Hashes don't interpolate in strings:

      #!perl -w use strict; my %SeqRec; print ("content of hash is %SeqRec\n");

      I recommend to use Data::Dumper for debugging contents of variables:

      use Data::Dumper; my %SeqRec = (foo => 'bar'); print ("content of hash is " . Dumper \%SeqRec);

        is the way of storing data I have mentioned into hash is correct because i dont see any data actually stored in it. the output showed as :

        contents of hash is $VAR1 = { '' => undef, 'foo' => 'bar'

      rkrish:

      A couple things:

      • Have you put a print statement inside your while loop to verify that results are coming back?
      • Have you tried using Data::Dumper to print the contents of $data in the loop?
      • Some databases will return the column names in upper case (I'm looking at you Oracle!).

      (Printing the contents of $data when read will show you if it's a case problem or not.)

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Hi, I have used print statement inside the while loop to print the content of data and also used Data::Dumper.The output is as follows

        data returned is HASH(0x4e9574) data returned is HASH(0x4e9598) data returned is HASH(0x4e958c) data returned is HASH(0x4e961c) data returned is HASH(0x4e95b0) data returned is HASH(0x4e955c) data returned is HASH(0x4e94fc) data returned is HASH(0x4e9568) data returned is HASH(0x4e95e0) data returned is HASH(0x4e96d0) data returned is HASH(0x22d410) data returned is HASH(0x4e94e4) data returned is HASH(0x22d350) data returned is HASH(0x4e9574) data returned is HASH(0x4e9598) data returned is HASH(0x4e958c) contents of hash is $VAR1 = { '' => undef, 'foo' => 'bar' };
        The output of query in sql is as follows:
        SQL> select seq_nbr,data_record from usage_inq; SEQ_NBR DATA_RECORD ------------------ ----- 200 10 145 10 205 10 450 10 2227 10 2386 10 202 10 204 10 144 10 472 10 1042 10 SEQ_NBR DATA_RECORD ------------------ ----- 1261 10 203 10 504 10 201 10 503 10 16 rows selected. SQL>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-07-14 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (253 votes), past polls