Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Challenge: sort weekdays in week-order (grep)

by rsFalse (Chaplain)
on Jul 25, 2022 at 21:46 UTC ( [id://11145726]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Challenge: sort weekdays in week-order (grep)
by LanX (Saint) on Jul 25, 2022 at 22:03 UTC
    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

        Note 'saxyzzy' and'Wen' in the output:

        Win8 Strawberry 5.30.3.1 (64) Tue 07/26/2022 6:46:09 C:\@Work\Perl\monks >perl use strict; use warnings; my $input = q/saxyzzy Monday Saturday Thursday Saturday Sat Sun Mon Tu +e Wen Th/; my $re = join "|", map ".*\\b($_\\S*)", map m/(..)/, qw/monday tuesday wednesday thursday friday saturday sunday/ ; print "\$re '$re' \n"; print join '.', $input =~ m/^(?|$re)\b(?{ print "$1\n" })(*FAIL)/i; ^Z $re '.*\b(mo\S*)|.*\b(tu\S*)|.*\b(we\S*)|.*\b(th\S*)|.*\b(fr\S*)|.*\b( +sa\S*)|.*\b(su\S*)' Mon Monday Tue Wen Th Thursday Sat Saturday Saturday saxyzzy Sun

        This can be avoided with more complete automatic pattern generation, but some false positives will remain, e.g., 'satu' for Saturday.

        File sort_weekdays_2.pl:

        False-positive day-name matches can be entirely avoided with a custom regex:

        my $rx_days = qr{ (?i) # custom patterns .*? \K \b mo (?: n (?: day)?)? | .*? \K \b tu (?: e (?: s (?: day)?)?)? | .*? \K \b we (?: d (?: nes day)?)? | .*? \K \b th (?: u (?: r (?: s (?: day)?)?)?)? | .*? \K \b fr (?: i (?: day)?)? | .*? \K \b sa (?: t (?: ur day)?)? | .*? \K \b su (?: n (?: day)?)? }xms;
        My somewhat arbitrary use of \K in this regex means the extraction regex must be changed. This works:
            $test =~ m{ \A $rx_days \b (?{ push @got, ${^MATCH} }) (*FAIL) }xmsp;
        Use of the ${^MATCH} match variable (and /p modifier) can be avoided by use of the
            substr $test, $-[0], $+[0]-$-[0]
        expression.


        Give a man a fish:  <%-{-{-{-<

        what about ... :)

        DB<189> p "@a" + Monday Tuesday Wednesday Thursday Friday Saturday Sunday DB<190> $a = join "", map { "(?=.*($_))*" } @a DB<191> x grep {defined} "Sunday Friday Monday" =~ /^$a/i 0 'Monday' + 1 'Friday' + 2 'Sunday' + DB<192>

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found