I have a list like this:
my $myV3VersionOfData = ["ZG","ZB","CXLDN",...];
and I want to convert it into a dictionary like this:
my $entries = {
'ZG' => {
'value' => 'ZG'
},
'ZB' => {
'value' => 'ZB'
},
'CXLDN' => {
'value' => 'CXLDN'
},
...
};
I tried this so far, but it doesn't work and gives me an error:
Can't use string ("ZG") as a HASH ref while "strict refs" in use at..
I understand this is occurring since I'm trying to assign the key value from the list, but how do I convert this list into a dictionary shown above?
my %genericHash;
for my $entry (@$myV3VersionOfData) {
$genericHash{ $entry->{key} } = $entry->{value};
}
How can I achieve this? I am new to Perl, and I have tried a bunch of things but it doesn't seem to work. Can anyone please help with this?
If you want to go through every element, to compute a new element, then you can use the map function. As map, can return multiple values, you return two values for each entry. And those can be converted to a hash.
my $array = ["ZG","ZB","CXLDN"];
my %hash = map { $_ => { value => $_ } } @$array;
my $hashref = { map { $_ => { value => $_ } } @$array };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With