I have an array with a bunch of strings as elements, and I want to put then into hash. So I spilt the strings in the array first and then put the parsed strings into a new array call parse_list.
This is my code:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Data::Dumper qw(Dumper);
my %hash;
my $string_array = [
"Europe West France Spain Germany",
"Europe North Finland Iceland",
"Asia East Japan Korea China",
"America North Mexico USA Canada"
];
foreach my $country(@{$string_array}){
my @parse_list = split(/\s+/, $country);
(my $continent,my $region,) = @parse_list[0,1];
#I just know how to get the continent and region, I don't know how to put
#the element from index [2..4] to an array
}
How can I set
Continent as the primary key and the Region as the value in the first layer of the hash.
Region as the secondary key and the array Country as the value in the second layer of the hash.
so it is like
my %hash = (
Europe => {
West => [ "France", "Spain", "Germany"]
}
);
my ($continent,$region,@rest) = @parse_list; $hash{$continent}{$region} = \@rest;