I'm going to write some code to implement your exact specification, but I'll say ahead of time that your specification doesn't make a lot of sense. I'll get to that.
#!/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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.