G'day IruP,
Welcome to the Monastery.
The following achieves what you ask; however, you've not been clear on what exact formats the keys, and values for the arrayrefs, take. So, the following code shows the basic technique; but, when you have a better specification, the specific elements of the code may well need to be changed.
#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @array = ('a', 1, 2, 3, 4, 'b', 6, 7, 8); my %hash; my $key; for my $element (@array) { if ($element =~ /^\D$/) { $key = $element; } else { push @{$hash{$key}}, $element; } } dd \%hash;
Output:
{ a => [1 .. 4], b => [6, 7, 8] }
See Data::Dump if you're unfamiliar with that module; the builtin Data::Dumper module has similar functionality.
— Ken
In reply to Re: Create a hash whose keys and values come from a given array
by kcott
in thread Create a hash whose keys and values come from a given array
by IruP
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |