in reply to Hash to Array assignment?
"I'm trying to understand how this can be rewritten in more simplistic syntax for better understanding."
In my opinion, some of your biggest barriers, with respect to making this more understandable, are the poor choice of variable names:
Beyond the naming issues, if you're encountering (associativity) difficulties with:
X = Y = Z
Then don't do it! This works the same and is abundantly clearer:
Y = Z X = Y
Consider these two versions of your code. As you've provided no context or other indication of the data involved, I've had to contrive names: I'm sure you can do better.
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; { # Contrived, arbitrary values for your variables my %hash1; my %hash2 = (b => [ 1, 2, 3 ]); my ($a, $b) = qw{a b}; # Your code my $sub = $hash1{$a} = $hash2{$b}; push @$sub, $a; # Your data print Dumper $sub; } { # Possibly more meaningful variable names (with same values) my %some_HoA; my %other_HoA = (b => [ 1, 2, 3 ]); my ($key_a, $key_b) = qw{a b}; # Possibly more understandable code $some_HoA{$key_a} = $other_HoA{$key_b}; my $some_array_ref = $some_HoA{$key_a}; push @$some_array_ref, $key_a; # More obvious data? print Dumper $some_array_ref; }
As you can see from the output, the functionality is the same in both pieces of code:
$VAR1 = [ 1, 2, 3, 'a' ]; $VAR1 = [ 1, 2, 3, 'a' ];
There are occasions (e.g. for reasons of efficiency) where hard-to-read, minimalist code is needed. These situations are not the norm; when they do arise, feel free to add copious comments to explain each and every nuance that you think may not be fully understood. Do bear in mind that code you write today may appear crystal clear; when you revisit it at some time in the (not necessarily distant) future, it may be less obvious than it once was.
-- Ken
|
|---|