0

So if i run this script it works the first time but when i try to add another value to the array and then assign the array back to the hash reference it gives me the value of ARRAY(0x9bd8254) as the values in the array.

the three lines in question

@addedvalue =  @{$state{$ustate}}{largest_city}  ;
push @addedvalue, $city;
$state{$ustate}{largest_city} =  \@addedvalue; 

I assume I'm doing something wrong in the last line. As i print out the array before i do the last line and it looks fine. But i am unable to determine what I am doing wrong. Any help would be great.

Thanks.

1
  • Use the Perl debugger to examine the values at each step. Post a copy of the debug session. Commented Feb 24, 2014 at 21:15

1 Answer 1

3

Hashes can only contains scalars, so you used a reference to an array (rather than array) as the value of the hash element.

But when it came time to print, you printed the value of the hash element rather than the elements of the array referenced by the value of the hash element.

$state{$ustate}{largest_city} = \@addedvalue; 
print("@{ $state{$ustate}{largest_city} }\n");

or

$state{$ustate}{largest_city} = \@addedvalue; 
print(join(', ', @{ $state{$ustate}{largest_city} }), "\n");

But it seems weird that you largest_city requires an array. The following would make more sense.

$state{$ustate}{largest_city} = $city; 
print("$state{$ustate}{largest_city}\n");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.