adding to an element to an array

Hi all,

I have created the following:
$site_array->{name} = $current_hash[0];
                    $site_array->{different} = false;
                    $site_array->{array} = [$current_hash[3]];
                    $site_version{$site_array->{name}} = $site_array;

Basically, what I'm trying to do is to have a hash pointing to an array and some scalar variables.  This seems to work when I first create it.  However, I want to push new elements onto the array later on in the program.  I've tried to do this, but what ends up happening is all the hashes end up pointing to the same array.  I want to keep all the arrays distinct.  Basically, I think this is a problem of scope.  Can someone please give me a hand?

Thanks!
mdigiovaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
$site_version{$site_array->{name}} = [@{$site_array->{array}}];
push @{$site_array->{array}},$newelement;
0
prakashk021799Commented:
use 'my' variables in the current scope.

For example:

my $hash;
for ('a'..'z') {
   my @array = (rand(100)) x 10;  # fill the array with 10 random elements
   $hash->{$_} = \@array;  # save the array ref in hash
}

The array is created afresh in each iteration. At the end of the loop you will have 26 elements in $hash, each pointing to a different array.
0
mdigiovaAuthor Commented:
Thanks for the answers, but I don't think it's what I need.  prakashk, I tried using "my" previously, but I ran into the same problem.  Is it possible I'm not using it correctly?  Here's the code I was trying:

my @temp_array = @{$site_version{$current_hash[0]}{array}};
push @temp_array, $current_hash[3];
@{$site_version{$current_hash[0]}{array}} = @temp_array

The array is already created, but later on, I want to add an element, so I'm trying to access the old array, push in a new value, then set it back, but it isn't working properly.  Any ideas?  Also, ozo, I create a bunch of hashes pointing to arrays initially so I don't think that I can use @{$site_array->{array} because it's only pointing to the array of the last hash that I've created.  If I want to add to an array in a previously created hash how would I do it?

Thanks again!
0
prakashk021799Commented:
For adding an element, you don't need to create a temp array.

push @{$site_version{$current_hash[0]}{array}}, $current_hash[3];

this should do it.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mdigiovaAuthor Commented:
Thanks prakashks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.