J
jere
After getting a good grasp on references (or at least i thought). I ran
into a simple problem which i cannot seem to get away from.
I have a complex data structure of hashes / arrays that I am setting up
before running a program. I noticed that several of the documents I
was parsing with this program shared the same attributes. I am using
the data structure as a guide - it tells me what to parse (and what not
to parse). Therefore, I need an element for each page i want to
parse, but i dont want to have to type the same attributes over and
over again. I created a hash with the common attributes and tried to
simply insert it into the hash. I might have several of these
"templates", so I created a hash to store them.
#here is hash (im going to try to insert
# %{ $fmt_template{outsrc_1}:
%fmt_template =
( 'outsrc_1' => {
System => 'end_sides',
DataRange => [ { R => 8, C => 0, r => 999, c => 3 },
{ R => 8, C => 5, r => 999, c => 8 } ],
Columns => [ 'sn', 'sn_net', 'host', 'desc' ]
}
);
# Here is a snippet of the array im trying to insert into:
%format = ( 'format_e' => {
Sheet => [
#Sheet 0: sheet name
{ System => 'step',
DataRange => [ { R => 2, C => 2, r => 41, c => 4 } ],
Columns => [ 'step', 'step2', 'step3' ]
}
#Sheet 1-4: these sheets are all the same and share similar
# attributes:
%format->{'format_excel'},
%format->{'format_excel'},
%format->{'format_excel'},
%format->{'format_excel'}
]
}
);
#so im on a particular sheet and i know its number, so i do:
#for loop for each Sheet (index via $i)
my $Fmt = $format{'format_e'}{Sheet}[$i];
#well, then i realized that there is one attribute that needed
#to be adjusted between the differnt sheets
#notice that the inserted hash has a r = 999. This represents max
#number of rows i could have, but i need to find the specific max #rows
of each document and compare. So i do the following:
#$x iterates through the number of DataRange arrays each
#Sheet[$i] contains:
if ( $Fmt->{DataRange}[$x]->{r} > $oSheet->{MaxRow} ) {
$Fmt->{DataRange}[$x]->{r} = $oSheet->{MaxRow};
}
#now ive changed the individual data range maxrow (r) element
# not true! ive changed them all due to using references??
# during the next for loop (next sheet) $Fmt->{DataRange}[$x]->{r}
# = 40 (maxrow of the previous sheet). It should be 999 again.
#Can someone point out what im doing wrong? I tried turning
#%format into a reference using $format = { hash } notation and #then
using {} braces arround "$format->{format_e}" in the larger
#hash. This works sytactically, but still when i change one, i
#change them all.
#I thought using a reference and THEN using a dereference would
#return a copy of each but that didnt work. I also tried just for
#fun the following
%format1 = %format
%format2 = %format
#and then tried using them in the code (references and not), but
#that still suffered from the same problem - it all seemed to be
#copies of the same memory location so when i changed one thing,
#they all changed.
#I even tried writing a small test program to test out different
#sytnaxes until i found something that worked, but this was
#fruitless.
into a simple problem which i cannot seem to get away from.
I have a complex data structure of hashes / arrays that I am setting up
before running a program. I noticed that several of the documents I
was parsing with this program shared the same attributes. I am using
the data structure as a guide - it tells me what to parse (and what not
to parse). Therefore, I need an element for each page i want to
parse, but i dont want to have to type the same attributes over and
over again. I created a hash with the common attributes and tried to
simply insert it into the hash. I might have several of these
"templates", so I created a hash to store them.
#here is hash (im going to try to insert
# %{ $fmt_template{outsrc_1}:
%fmt_template =
( 'outsrc_1' => {
System => 'end_sides',
DataRange => [ { R => 8, C => 0, r => 999, c => 3 },
{ R => 8, C => 5, r => 999, c => 8 } ],
Columns => [ 'sn', 'sn_net', 'host', 'desc' ]
}
);
# Here is a snippet of the array im trying to insert into:
%format = ( 'format_e' => {
Sheet => [
#Sheet 0: sheet name
{ System => 'step',
DataRange => [ { R => 2, C => 2, r => 41, c => 4 } ],
Columns => [ 'step', 'step2', 'step3' ]
}
#Sheet 1-4: these sheets are all the same and share similar
# attributes:
%format->{'format_excel'},
%format->{'format_excel'},
%format->{'format_excel'},
%format->{'format_excel'}
]
}
);
#so im on a particular sheet and i know its number, so i do:
#for loop for each Sheet (index via $i)
my $Fmt = $format{'format_e'}{Sheet}[$i];
#well, then i realized that there is one attribute that needed
#to be adjusted between the differnt sheets
#notice that the inserted hash has a r = 999. This represents max
#number of rows i could have, but i need to find the specific max #rows
of each document and compare. So i do the following:
#$x iterates through the number of DataRange arrays each
#Sheet[$i] contains:
if ( $Fmt->{DataRange}[$x]->{r} > $oSheet->{MaxRow} ) {
$Fmt->{DataRange}[$x]->{r} = $oSheet->{MaxRow};
}
#now ive changed the individual data range maxrow (r) element
# not true! ive changed them all due to using references??
# during the next for loop (next sheet) $Fmt->{DataRange}[$x]->{r}
# = 40 (maxrow of the previous sheet). It should be 999 again.
#Can someone point out what im doing wrong? I tried turning
#%format into a reference using $format = { hash } notation and #then
using {} braces arround "$format->{format_e}" in the larger
#hash. This works sytactically, but still when i change one, i
#change them all.
#I thought using a reference and THEN using a dereference would
#return a copy of each but that didnt work. I also tried just for
#fun the following
%format1 = %format
%format2 = %format
#and then tried using them in the code (references and not), but
#that still suffered from the same problem - it all seemed to be
#copies of the same memory location so when i changed one thing,
#they all changed.
#I even tried writing a small test program to test out different
#sytnaxes until i found something that worked, but this was
#fruitless.