I've been searching perlmonks for pretty long now, also tried different resources on the web, but haven't found anything so far.
What I'm doing:
i'm building an array with all possible combinations of a word list (code of combination sub found on perlmonks).
i then get the array @combos back, which i need sorted by value length of the array. Getting it sorted by value is not the problem, but by length of the value of each entry.
my @array;
push @array, "one";
push @array, "two";
push @array, "three";
psuh @array, "four";
my $k=0;
my @combos;
for(combinations(@array))
{
$combos[$k] = join("|",@$_);
$k++;
}
sub combinations {
return [] unless @_;
my $first = shift;
my @rest = combinations(@_);
return @rest, map { [$first, @$_] } @rest;
}
this returns the following array:
$VAR1 = [
'',
'four',
'three',
'three|four',
'two',
'two|four',
'two|three',
'two|three|four',
'one',
'one|four',
'one|three',
'one|three|four',
'one|two',
'one|two|four',
'one|two|three',
'one|two|three|four'
];
but what I need is to sort the array, so the return value is something like:
$VAR1 = [
'one|two|three|four'
'two|three|four',
'one|three|four',
'one|two|three',
'one|two|four',
'three|four',
'two|three',
'one|three',
'two|four',
'one|four',
'one|two',
'three',
'four',
'two',
'one',
''
];
any help regarding this would be greatly appreciated.
Emanuel
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.