Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by roho (Bishop)
on Jul 23, 2022 at 21:45 UTC ( [id://11145672]=note: print w/replies, xml ) Need Help??


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

++tybalt89!! That was a very clever use of "tr" and its options. I added a small tweak using unshift and pop to put Sunday first.

#!/usr/bin/perl use strict; use warnings; use List::AllUtils qw( shuffle sort_by ); my @weekdays = shuffle qw/Monday Tuesday Wednesday Friday Sunday Satur +day Thursday/; @weekdays = @{[ sort_by { lc =~ tr/muwhft/a-e/dr } @weekdays ]}; unshift(@weekdays,pop(@weekdays)); print "@weekdays\n";

"It's not how hard you work, it's how much you get done."

  • Comment on Re^3: Challenge: sort weekdays in week-order (elegantly and efficiently)
  • Download Code

Replies are listed 'Best First'.
Re^4: Challenge: sort weekdays in week-order (elegantly and efficiently)
by tybalt89 (Monsignor) on Jul 23, 2022 at 22:40 UTC

    No need for that, just tweak the "tr" a little :)

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11145633 use warnings; use List::AllUtils qw( shuffle sort_by ); my @weekdays = shuffle qw/Monday tuesday wednesday friday sunday Saturday Thursday/; print "$_\n" for sort_by { lc =~ tr/muwht/a-d/dr } @weekdays; # Monday + first print "\n"; print "$_\n" for sort_by { lc =~ tr/nmewhfatsu/a-g/dr } @weekdays; # S +unday first
      Should also work:
      print "$_\n" for sort_by { lc =~ tr/hfst/x-z/dr } @weekdays; # Monday +first print "\n"; print "$_\n" for reverse sort_by { scalar reverse lc =~ tr/rn/at/dr } +@weekdays; # Sunday first
      Nice! It just shifted from magic to deep magic. :)

      "It's not how hard you work, it's how much you get done."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-18 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found