Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append data from one Hash Array to Another Hash Array...

Status
Not open for further replies.

rkumar28

MIS
Jan 30, 2005
15
US
Hi,

I have three functions as below. The two functions get_monthly_data() and get_daily_data() gets Monthly and Daily related data into their respective
Hash array(see the function below). Both the functions has same fields.
All I am trying to do is to merge(append the data from one hash to another hash). Put data from the two hash array of the two functions above into a third hash array.


#!/usr/bin/perl
$monthly_data = get_monthly_data();
$daily_data = get_daily_data();
$merge_data = get_merge_data( $monthly_data , $daily_data )

The $merge_data needs to have data from both $monthly_data , $daily_data

sub get_merge_data
{
How to append data from get_monthly_data() hash array and get_daily_data() hash array into a third Hashand return it from this function.

}



sub get_monthly_data
{
$dbh = DBI->connect("dbi:Oracle:$dbname", .....Database Connectivity......)
$cmd =
q { SELECT DISTINCT
ID, NAME, STATUS, SRC FROM TABLE A };

std = $dbh->prepare("$cmd");
$std->execute;
$hash_return = {};
$node_index=0;

while ( $hash_ref = $std->fetchrow_hashref )
{
push @{$hash_return->{$hash_ref->{'ID'}}}, $hash_ref;
$node_index++;
}
$std->finish;
$dbh->disconnect;
return($hash_return);
}

sub get_daily_data
{
$dbh = DBI->connect("dbi:Oracle:$dbname", .....Database Connectivity......)
$cmd =
q { SELECT DISTINCT
ID, NAME, STATUS, SRC FROM TABLE B };
std = $dbh->prepare("$cmd");
$std->execute;
$hash_return = {};
$node_index=0;
while ( $hash_ref = $std->fetchrow_hashref )
{
push @{$hash_return->{$hash_ref->{'ID'}}}, $hash_ref;
$node_index++;
}
$std->finish;
$dbh->disconnect;
return($hash_return);
}



 
For those wishing to help rkumar28 out, I've run his code through perltidy:

Code:
#!/usr/bin/perl
$monthly_data = get_monthly_data();
$daily_data   = get_daily_data();
$merge_data   = get_merge_data( $monthly_data, $daily_data );

[b]# The $merge_data needs to have data from both $monthly_data , $daily_data [/b]

sub get_merge_data {

[b]# How to append data from get_monthly_data() hash array and get_daily_data() hash array into a third Hashand return it from this function.  [/b]
}

sub get_monthly_data {
    $dbh = DBI->connect( "dbi:Oracle:$dbname", .....Database Connectivity...... );
    $cmd = q{SELECT DISTINCT ID, NAME, STATUS, SRC FROM TABLE A};

    std = $dbh->prepare("$cmd");
    $std->execute;
    $hash_return = {};

    while ( $hash_ref = $std->fetchrow_hashref ) {
        push @{ $hash_return->{ $hash_ref->{'ID'} } }, $hash_ref;
    }
    $std->finish;
    $dbh->disconnect;
    return ($hash_return);
}

sub get_daily_data {
    $dbh = DBI->connect( "dbi:Oracle:$dbname", .....Database Connectivity...... );
    $cmd = q{SELECT DISTINCT ID, NAME, STATUS, SRC FROM TABLE B};
    
    std = $dbh->prepare("$cmd");
    $std->execute;
    $hash_return = {};
    
    while ( $hash_ref = $std->fetchrow_hashref ) {
        push @{ $hash_return->{ $hash_ref->{'ID'} } }, $hash_ref;
    }
    $std->finish;
    $dbh->disconnect;
    return ($hash_return);
}
 
Ok, let's separate the tasks you have: You are running
a query and getting a datastructure
%{$hash_return}={key1=>[{reference to hash1},{%reference to hash2},....,{reference to hashN1}];

my guess is what you are trying to do with the second sql is
append to $hash_return->{$key1}, so that at the end of the day you want
%{$hash_return}={key1=>[{reference to hash1},{%reference to hash2},....,{reference to hashN1},{reference to secondhash1},{%reference to secondhash2},....,{reference to secondhashN2];


This would mean coding like this for the second sql fetch
(I assume here that at the end of the day everything will be stored in a single hash; if you want to save them separately, you can do that on the fly.
So I really do not see the need for two separate reads
and definitely not for a merge function; you can do everything on the fly)
...reading the second sql fetch
while ( $hash_ref = $std->fetchrow_hashref ) {
push @{ $hash_return->{ $hash_ref->{'ID'} } }, $hash_ref;
}

should do it. Just use the same hash_return reference for both monthly and daily
Of course I may have misunderstood the question....
 
Thanks Miller and Svar for replying. Thanks for the suggestion. Yes...I am trying to store eveything in single hash. I think what you are suggesting make sense and is more efficient. I will remove the merge statement.

Please correct me if I understood it correctly.I have to write both queries in a single function. Something like below (please see get_monthly_daily_data() function) .

I am not sure if I should post this separately as a new topic. My two sql queries are very long. Each are about two and half pages. I am planning to write these queries in two separate files test1.sql and test2.sql and call these files in my $cmd1 and $cmd2 below.... If possible...can you please suggest how to pass the sql from the two files into $cmd1 and $cmd2.



sub get_monthly_daily_data {
$dbh = DBI->connect( "dbi:Oracle:$dbname", .....Database Connectivity...... );

$hash_return = {};


$cmd1 = q{SELECT DISTINCT ID, NAME, STATUS, SRC FROM TABLE A};
std1 = $dbh->prepare("$cmd1");
$std1->execute;
while ( $hash_ref = $std1->fetchrow_hashref ) {
push @{ $hash_return->{ $hash_ref->{'ID'} } }, $hash_ref;
}
$std1->finish;
#### Query One finishes here ####



#### Query Two Starts here ####
$cmd2 = q{SELECT DISTINCT ID, NAME, STATUS, SRC FROM TABLE B};
std2 = $dbh->prepare("$cmd2");
$std2->execute;
while ( $hash_ref = $std2->fetchrow_hashref ) {
push @{ $hash_return->{ $hash_ref->{'ID'} } }, $hash_ref;
}
$std2->finish;


$dbh->disconnect;
return ($hash_return);
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top