#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @nlist = qw( 3 24 8 17 23 14 17 9 16 24 25 11 22 14 14 8 19 16 15 8 + ); my @key_list = ( 'A' .. 'Z' ); my %hoa; foreach my $num ( @nlist ) { my $key; foreach my $check_key ( @key_list ) { if( not exists $hoa{ $check_key } or $num % $hoa{ $check_key }[ 0 ] == 0 ) { $key = $check_key; last; } } die "Key list depleted" if not defined $key; push @{ $hoa{ $key } }, $num; } print Dumper \%hoa;
My problem with the algorithm you outlined, though, is that if the list contains a 9 earlier than a 3, then 9 will be considered a denominator different from 3. Is that what you intend?
If not, then the solution is to test the candidate denominator and the number against each other both ways, and to make sure to keep the smallest number under each key at the front of the list. (Except remembering to make 1 a special case, that is.) In that case you will get your input list segmented by the smallest prime factor products present in the list.
Makeshifts last the longest.
In reply to Re: Howto Dynamically Create a Hash?
by Aristotle
in thread Howto Dynamically Create a Hash?
by monkfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |