in reply to Regex Substring SORT Conundrum
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; my %order = ( Numbers => 1, Psalm => 2, Matthew => 3, Luke => 4, John => 5, Acts => 6, '2 Corinthians' => 7, Ephesians => 8, Philippians => 9, Colossians => 10, Hebrews => 11, '1 Peter' => 12, ); sub biblically { my @info = map [/(.*?) ([0-9]+):([0-9]+)/], $a, $b; $order{$info[0][0]} <=> $order{$info[1][0]} || $info[0][1] <=> $info[1][1] || $info[0][2] <=> $info[1][2] } my @unsorted = split /\n/, << '__LIST__'; Acts 4:29-31 Numbers 14:10 Luke 1:20 John 16:15 Acts 2:4 1 Peter 1:22 Psalm 56:3 2 Corinthians 12:2, 4, 1, 11 Ephesians 3:18, 19 Ephesians 4:14, 13, 17, 18; 5:15, 16 Matthew 24:24 Colossians 2:6-8 Hebrews 10:35-39 Hebrews 4:10-12 Philippians 1:6, 27-29 Matthew 7:6-12, 15 Philippians 2:13-15 __LIST__ my @sorted = sort biblically @unsorted; say for @sorted;
If the lists are very long, you might preprocess the input by so called Schwartzian transform:
sub biblically { $order{$a->[0]} <=> $order{$b->[0]} || $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } my @sorted = map $_->[-1], sort biblically map [/(.*?) ([0-9]+):([0-9]+)/, $_], @unsorted;
You didn't specify how to sort citations like
Ephesians 3:18, 19 Ephesians 3:18-21
To handle all such cases, the biblically subroutine might get even more complicated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex Substring SORT Conundrum
by Polyglot (Chaplain) on Mar 05, 2015 at 16:33 UTC | |
by choroba (Cardinal) on Mar 05, 2015 at 16:56 UTC |