Hash/array reference question

B

Bryan

If I have a hash reference, and want to assign a reference to a snapshot
of a changing array, what is the correct syntax?

$hash_ref->{'key'} = @array;
Obviously doesn't work

$hash_ref->{'key'} = \@array;
Doesn't work since my array is changed then reused for different keys,
the values get hosed.

my @tmp_array = @array;
$hash_ref->{'key'} = \@tmp_array;
Works as I want, but now I have an extra line of code.

Is that really necessary? Seems a bit clumsy to have to make a local
copy explicitly like that...

Thanks,
B
 
P

Paul Lalli

If I have a hash reference, and want to assign a reference to a snapshot
of a changing array, what is the correct syntax?

$hash_ref->{'key'} = @array;
Obviously doesn't work

$hash_ref->{'key'} = \@array;
Doesn't work since my array is changed then reused for different keys,
the values get hosed.

my @tmp_array = @array;
$hash_ref->{'key'} = \@tmp_array;
Works as I want, but now I have an extra line of code.

Is that really necessary? Seems a bit clumsy to have to make a
local copy explicitly like that...

Create a reference to a new anonymous array, and populate that array
with the current values of @array:

$hash_ref->{key} = [ @array ];

Paul Lalli
 
B

Bryan

Paul said:
If I have a hash reference, and want to assign a reference to a snapshot
of a changing array, what is the correct syntax?

$hash_ref->{'key'} = @array;
Obviously doesn't work

$hash_ref->{'key'} = \@array;
Doesn't work since my array is changed then reused for different keys,
the values get hosed.

my @tmp_array = @array;
$hash_ref->{'key'} = \@tmp_array;
Works as I want, but now I have an extra line of code.

Is that really necessary? Seems a bit clumsy to have to make a
local copy explicitly like that...

Create a reference to a new anonymous array, and populate that array
with the current values of @array:

$hash_ref->{key} = [ @array ];

Paul Lalli

Ah, cool, thanks.

B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top