Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Sorting Strings By Vowel Sequence

by Anonymous Monk
on Nov 18, 2014 at 04:09 UTC ( [id://1107525]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Sorting Strings By Vowel Sequence
in thread Sorting Strings By Vowel Sequence

You can combine many sorts. Here's something quick and dirty:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @words = qw( xaxexix babebib xaxexi babebibb baebbib ); sub prepare_sort { my ( $vowels, $positions ) = ( '', '' ); while (/([aeiou])/g) { $vowels .= $1; $positions .= sprintf '%03d', pos; } return [ $_, $positions, $vowels ]; } say for map $_->[0], sort { $a->[2] cmp $b->[2] } # vowels sort sort { $a->[1] cmp $b->[1] } # positional sort sort { $a->[0] cmp $b->[0] } # alphabetical sort map prepare_sort(), @words;
Output:
baebibb babebib babebibb xaxexi xaxexix

Replies are listed 'Best First'.
Re^4: Sorting Strings By Vowel Sequence
by Anonymous Monk on Nov 18, 2014 at 04:31 UTC
    Now that I think of it, it can be greatly simplified:
    sub prepare_sort { return [ $_, tr/aeiou/z/cr, tr/aeiou//dr ]; }
    Thanks to Zucan for the tip!

    So we dont need a sub now:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @words = qw( xaxexix babebib xaxexi babebibb baebbib ); say for map $_->[0], sort { $a->[2] cmp $b->[2] } # vowels sort sort { $a->[1] cmp $b->[1] } # positional sort sort { $a->[0] cmp $b->[0] } # alphabetical sort map [ $_, tr/aeiou/z/cr, tr/aeiou//dr ], @words;
      map [ $_, tr/aeiou/ /r, tr/aeiou//cdr ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1107525]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found