in reply to Challenge: sort weekdays in week-order (elegantly and efficiently)

maybe?

use v5.12; use warnings; use Data::Dump qw/pp dd/; my $input = join "|", qw/Monday Saturday Thursday/; my @order = qw/monday tuesday wednesday thursday friday saturday sunda +y/; pp grep { /^($input)$/i } @order;

==>

("monday", "thursday", "saturday")

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Challenge: sort weekdays in week-order (grep)
by rsFalse (Chaplain) on Jul 25, 2022 at 21:46 UTC
    Similarly,
    #!/usr/bin/perl -l use warnings; use strict; my $input = join "|", qw/Monday Saturday Thursday/; "monday tuesday wednesday thursday friday saturday sunday" =~ m/\b($in +put)\b(?{ print "[$1]" })(*FAIL)/i; print join ' ', grep s/,//, split ' ', "monday tuesday wednesday thurs +day friday saturday sunday" =~ s/\b($input)\b\K/,/igr;
    [monday] [thursday] [saturday] monday thursday saturday
      cool! :)

      hm theoretically it should be possible to swap it and generate a (complicated) regex from "monday .. sunday" which sorts the input without explicit sort like the others showed.

      But I'm better opting out now! =)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I find a way to do so only with searching from the beginning of the string ("^.*"):
        my $input = q/Monday Saturday Thursday Saturday Sat Sun Mon Tue Wen Th +/; my $re = join "|", map ".*\\b($_\\S*)", map m/(..)/, qw/monday tuesday + wednesday thursday friday saturday sunday/; print join ' ', $input =~ m/^(?|$re)\b(?{ print "$1\n" })(*FAIL)/i;
        Mon Monday Tue Wen Th Thursday Sat Saturday Saturday Sun