Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by choroba (Cardinal)
on Jul 22, 2022 at 09:50 UTC ( [id://11145648]=note: print w/replies, xml ) Need Help??


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

This still uses the %order, but populates it via a core module:
use Time::Piece; my %order = map { split ' ', 'Time::Piece'->strptime("$_", "%d")->strftime('%A %u') } 1 .. 7;
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Challenge: sort weekdays in week-order -- Lokiday
by Discipulus (Canon) on Jul 22, 2022 at 10:21 UTC
    bravo choroba!

    I was trying something similar but I easily get annoyed by strptime et similia...

    Infact even with your code I hit Error parsing time at C:/perl5.26.64bit/perl/lib/Time/Piece.pm line 581.

    I'd modify your code to be even more stable, in the case they accept my proposal for 8 days week, adding Lokiday as jolly day free for all :D

    my %order = map { split ' ', 'Time::Piece'->strptime("$_", "%d")->strftime('%A %u') } Time::Piece::day_list();

    bliako: your solution is already elegant, readable and perlish: dont overcomplicate :)

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      > even with your code I hit Error parsing time at C:/perl5.26.64bit/perl/lib/Time/Piece.pm line 581.

      That's weird, I'm running it without errors on 5.26.1 (Time::Piece 1.31).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        I remebemer weird results from strptime and strftime on windows but atm I cannot find the thread, I'll msg you and update the thread if i find something

        This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x +64-multi-thread $Time::Piece::VERSION 1.3201

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: Challenge: sort weekdays in week-order (Time::Piece)
by LanX (Saint) on Jul 22, 2022 at 12:17 UTC
    FWIW
    > perl -de0 DB<38> use Time::Piece DB<39> x Time::Piece::day_list 0 'Sun' 1 'Mon' 2 'Tue' 3 'Wed' 4 'Thu' 5 'Fri' 6 'Sat' DB<40> x @{Time::Piece::_get_localization->{weekday}} 0 'Sunday' 1 'Monday' 2 'Tuesday' 3 'Wednesday' 4 'Thursday' 5 'Friday' 6 'Saturday'

    And if you don't like the traditional order

    DB<41> @W = @{Time::Piece::_get_localization->{weekday}} DB<42> push @W, shift @W DB<43> x @W 0 'Monday' 1 'Tuesday' 2 'Wednesday' 3 'Thursday' 4 'Friday' 5 'Saturday' 6 'Sunday' DB<44>

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

      Hmm... Not present in 5.26.1, works but not documented in 5.37.1. I'm not sure I'd use it.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        oh I see _get_localization is new in Time::Piece

        I just needed it anyway only to access the private

        my @FULLDAY_LIST = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

        There are some (ugly) hacks to get it directly tho ...

        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://11145648]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found