djbryson has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to build a hash through a loop. I think i'm close... I tried push, and dot equals.
my $org_id; my $org_nm; my $dbh=DBI->connect('DBI:Oracle:xxx', 'yyy', 'zzz') or die "Cannot co +nnect to DB: " . DBI->errstr; my $sql = "SELECT org_id,org_name FROM mv_org_structure_list WHER +E ORG_TYPE='D'"; my $sth = $dbh->prepare($sql) || print "Cannot prepare"; $sth->execute || print "Cannot execute"; my @result; my %hasho; while (@result = $sth->fetchrow_array()) { $org_id = $result[0]; $org_nm = $result[1]; %hasho = ("$result[0]", "$result[1]"); }

Replies are listed 'Best First'.
Re: push/append to hash in a loop
by kyle (Abbot) on Mar 20, 2007 at 20:51 UTC

    I think you want:

    $hasho{ $result[0] } = $result[1];

    You could also get DBI to do the work for you. Replace your loop with:

    my %hasho = %{ $sth->fetchall_hashref( 'org_id' ) };

    This will produce something a little different than you're expecting, though. Each entry in %hasho will be a hashref with two keys ('org_id' and 'org_name').

    If you're willing to go that route, you can replace the prepare and execute with:

    my %hasho = %{ $dbh->selectall_hashref( $sql, 'org_id' ) };
      thank you
Re: push/append to hash in a loop
by johngg (Canon) on Mar 20, 2007 at 23:29 UTC
    kyle has shown you how to add key/value pairs to your hash. It might help you to show where it would be applicable to use push and dot equals. The push operator is used to add an element to the right-hand or high end of an array, like this

    $ perl -le ' > @arr = qw{one two three}; > print qq{@arr}; > push @arr, q{four}; > print qq{@arr};' one two three one two three four $

    Whereas the .= assignment operator (see perlop) is used to concatenate strings, like this

    $ perl -le ' > $str = q{abcde}; > print $str; > $str .= q{fgh}; > print $str;' abcde abcdefgh $

    I hope you find this useful.

    Cheers,

    JohnGG

Re: push/append to hash in a loop
by agianni (Hermit) on Mar 21, 2007 at 00:58 UTC

    Consider using fetchrow_hashref instead of fetchrow_arrayref:

    while ( my $row = $sth->fetchrown_hashref ){ $hasho{ $row{org_id} } = $row{org_nm}; }

    If you really need to use fetchrow_array for some reason, you can just dump the results directly into variables:

    while ( my ( $org_id, $org_nm ) = $sth->fetchrow_array()) { $hasho{$org_id} = $org_nm; }

    Or, if you want to get real Perl-ish:

    my %hash = map { $_->{org_id} => $_->{org_nm} } @{ $sth->fetchall_arra +yref };

    Also, no need for the quoting of variables on the last line of your code.

    split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(join(q{},map +{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79*36997,13 +*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));
      If you really need to use fetchrow_array for some reason

      A good reason is that it's faster (update: erm: fetchrow_arrayref I mean. fetchrow_array is a fetch with training wheels). In my experience, if I want to do something hashy with the results of a DB fetch, it's usually faster to do exactly what I want in a loop over fetchrow_arrayref, rather than fetchrow_hashref.

      That is, you don't always need the entire row stored as key/value pairs. Usually all you need are a couple of key/value pairs (that usually correspond to a unique index tuple) and the rest of the values you can store in a list.

      And that list of values winds up being formatted in a simple join or join( '...', map). The cost of uselessly hashifying these values is what hurts.

      • another intruder with the mooring in the heart of the Perl

Re: push/append to hash in a loop
by anirudh_sml (Novice) on Mar 21, 2007 at 07:04 UTC
    Hi,
    What i am able to understand from your code that you are trying to make a hash by fetching a data from a query .
    Try this way also :
    my $get_details = ''; my $get_data; my $query = qq|write your query here ;|; $get_details = $self->{_dbh}->prepare(qq{$query});#_dbh is a object de +fined in your code u r doing it right . $get_details->execute($variables,$needed);# if no variable is need +ed in the query then leave it empty while(my $details_dump = get_details->fetchrow_hashref){ push(@{$get_data}, $details_dump); } $get_details->finish; return $get_data; #if you are using a it in a function this is re +quired hash.
    I think this will help you .
    Thanks & regards