My solution isn't as elegant as the other algorithms already suggested, however, I think mine is more straight-forward, especially if you are still new to Perl:
#!/usr/bin/perl -w use strict; my @words = qw(fanfare apparate panacea parmesan albatross albacore false vermeil candelabra beans); my %hash; foreach my $word (@words) { my $vowels = lc $word; $vowels =~ s/[^aeiou]//g; # EXAMPLE B my @array = ($word); push @array, @{$hash{$vowels}} if defined $hash{$vowels}; $hash{$vowels} = \@array; } my @sorted; foreach my $vowels (sort keys %hash) { push @sorted, @{$hash{$vowels}}; # EXAMPLE A } print " INPUT: @words\n"; print "SORTED: @sorted\n";

The above code provides identical output to what was suggested in the original question.

For the edge case where words have the same vowels in the same positions, there may be a desire to sort the words in alphabetical order. The above code would take the words "xaxexix babebib" (in that order) and print "xaxexix babebib". To make the code print "babebib xaxexix" instead, change the line marked "EXAMPLE A" to the following:
push @sorted, sort @{$hash{$vowels}};
For the edge case where words have the same vowels in the same order but with differing positions, there may be a desire to have the a vowel that shows up sooner in a word to sort higher than a word that has the vowel show up later in a word. For example, the above code would take the words "babebib baebbib" (in that order) and print "babebib baebbib". To make the code print "baebbib babebib" instead, change the line marked "EXAMPLE B" to the following:
$vowels =~ s/[^aeiou]/z/g;
We essentially replace all the non-vowels with "z", preserving the position of the vowels.

This was a great question. It is great to see the different ways a problem can be solved in Perl!


In reply to Re: Sorting Strings By Vowel Sequence by Zucan
in thread Sorting Strings By Vowel Sequence by NewToPerl777

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.