Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Array of dynamic Hash how to preserve the values

by dwm042 (Priest)
on Mar 25, 2009 at 17:55 UTC ( [id://753196]=note: print w/replies, xml ) Need Help??


in reply to Array of dynamic Hash how to preserve the values

Core problem is too much indirection, which means you are making references back to an original when you probably intend to make copies. I won't dig too deep into this, but I'll show code that illustrates the problem.

This code just makes pointers to a single version of the structure, without making copies of the structure's changes.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @data=({}); my %work_request = ( "DR"=>[0,0,0,0,0], "SAT ITAMS"=>[0,0,0,0,0], "PROD ITAMS"=>[0,0,0,0,0] ); #push @data, [ %work_request ]; push @data, \%work_request; $work_request{DR} = [1,2,3,4]; #push @data, [ %work_request ]; push @data, \%work_request; print Dumper( @data );
and the output is:

C:\Code>perl how_many_indirects.pl $VAR1 = {}; $VAR2 = { 'PROD ITAMS' => [ 0, 0, 0, 0, 0 ], 'SAT ITAMS' => [ 0, 0, 0, 0, 0 ], 'DR' => [ 1, 2, 3, 4 ] }; $VAR3 = $VAR2;


This version of code copies the unique components as the base structure changes.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @data=({}); my %work_request = ( "DR"=>[0,0,0,0,0], "SAT ITAMS"=>[0,0,0,0,0], "PROD ITAMS"=>[0,0,0,0,0] ); push @data, [ %work_request ]; #push @data, \%work_request; $work_request{DR} = [1,2,3,4]; push @data, [ %work_request ]; #push @data, \%work_request; print Dumper( @data );
and the output is:

C:\Code>perl how_many_indirects.pl $VAR1 = {}; $VAR2 = [ 'PROD ITAMS', [ 0, 0, 0, 0, 0 ], 'SAT ITAMS', [ 0, 0, 0, 0, 0 ], 'DR', [ 0, 0, 0, 0, 0 ] ]; $VAR3 = [ 'PROD ITAMS', $VAR2->[1], 'SAT ITAMS', $VAR2->[3], 'DR', [ 1, 2, 3, 4 ] ]; C:\Code>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found