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:
How to deep copy hash to a hash

 



Tejas
User

Nov 13, 2014, 5:20 AM

Post #1 of 3 (2338 views)
How to deep copy hash to a hash Can't Post

I am creating a multi level hash and trying to send it as an arguement .
But it isnt working in the function.

Its empty.Probably the reference is being copied.

How to access the values of the original has in func

Code
if($company eq '04'|| $company eq  '3S'|| $company_code eq '2R'||$company eq '06'||$company eq '79') { 
$retail_hash{$amz_company_code}{$automated_manual}{$subledger_source}{$source_system}{$categorized}=
(defined $retail_hash{$amz_company_code}{$automated_manual}{$subledger_source}{$source_system}{$categorized} ?
$retail_hash{$amz_company_code}{$automated_manual}{$subledger_source}{$source_system}{$categorized}:0) + $transaction_document_amount;
}

print_txn_wise_hash("Retail",%retail_hash);
sub print_txn_wise_hash
{
my $txn_type = shift;
my %gl_hash = {shift};
print Dumper(\%gl_hash);
}




Output

Quote
$VAR1 = {};




Quote

Actaully Iam Creating Several Hashses in similar fashion and prinring them one by one through func calls

print_txn_wise_hash("Retail",%retail_hash);
print_txn_wise_hash("Material",%Material_hash);
print_txn_wise_hash("Actual",%actual_hash);


But In function the hash is referenced to null.
\How to make it poiint to original hash.


(This post was edited by Tejas on Nov 13, 2014, 5:26 AM)


BillKSmith
Veteran

Nov 13, 2014, 7:19 AM

Post #2 of 3 (2332 views)
Re: [Tejas] How to deep copy hash to a hash [In reply to] Can't Post

You do not need "deep copy". After you shift of $txt_type, @_ contains the high level hash (all the values are references that refer to the anonymous hashes in the main program.)


Code
my %gl_hash = @_;



When you are passing more than one argument, it is better to pass arrays and hashes by reference. The avoids problems with "flattening".


Code
print_txn_wise_hash("Retail",\%retail_hash);  
sub print_txn_wise_hash {
my $txn_type = shift;
my %gl_hash = %{shift};
print Dumper(\%gl_hash);
}

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Nov 13, 2014, 10:48 AM

Post #3 of 3 (2318 views)
Re: [BillKSmith] How to deep copy hash to a hash [In reply to] Can't Post

Consider this session under the Perl debuger:

Code
  DB<1>  use Data::Dumper; 

DB<2> %hash = qw / jan 1 feb 2 mar 3/;

DB<3> sub print_hash { %h = @_; print Dumper \%h}

DB<4> print_hash(%hash);
$VAR1 = {
'jan' => '1',
'feb' => '2',
'mar' => '3'
};

DB<5> sub print2 { %h = {shift}; print Dumper \%h}

DB<6> print2(%hash); # does not work
$VAR1 = {
'HASH(0x6004f1508)' => undef
};

DB<7> sub print3 { $h_ref = shift; print Dumper $h_ref;}

DB<8> print3(\%hash)
$VAR1 = {
'feb' => '2',
'jan' => '1',
'mar' => '3'
};

DB<9>

On line 3, I assign the full @_ array to the hash, and the printing on line 4 goes OK.

On line 5, I used your syntax, and it only create an empty (undefined) hashref, as seen in the call to the print2 subroutine. The {} syntax is for creating a hash reference, and shift will take only the first element of the @_ array.

On lines 7 and 8, I use explicitly a hash ref and that works OK.

 
 


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

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