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

Re^2: The date last monday

by Scarborough (Hermit)
on Oct 03, 2006 at 10:59 UTC ( [id://576034]=note: print w/replies, xml ) Need Help??


in reply to Re: The date last monday
in thread The date last monday

I know and in production i have a sub routine to format as 20061002 but my challange is to do the formating and calulation in one line. Heres my sub
sub get_CCYYMMDD{ my $timecode = $_[0]; my ($day, $month, $year) = (localtime($timecode))[3..5]; $year += 1900; $month++; $month = $month<10 ? "0".$month : $month; $day = $day<10 ? "0".$day : $day; my $today = $year.$month.$day; #print $today."\n"; return $today; }

Am I asking to much to do this all in one go?

Replies are listed 'Best First'.
Re^3: The date last monday
by davorg (Chancellor) on Oct 03, 2006 at 11:26 UTC

    You seem to have written a rather less flexible version of POSIX::strftime :-)

    use POSIX 'strftime'; sub get_CCYYMMDD { return strftime '%Y%m%d', $_[0]; }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I have never investigated POSIX maybe its time I did. Thanks for your advice
Re^3: The date last monday
by Old_Gray_Bear (Bishop) on Oct 03, 2006 at 11:33 UTC
    "Am I asking to much to do this all in one go?"

    Well. no, as previous posters have demonstrated. On the other hand, if I were to encounter a one-liner 'solution' in a code-review I'd flag it as a 'Maintenance problem'. The one-liner is cute and quirky and flashy. But the purpose of code is to be readable at 0300 by a half-asleep bloke who just got to the pager before it woke the Baby. I vote for simple and verbose every time.

    ----
    I Go Back to Sleep, Now.

    OGB

      I'm with you, the orginal verbose version will stay in the code. However I have enjoyed the Monks contributions on this subject and from setting myself the challange have learnt about POSIX and map and how to pad sprinf which was not in the nutshell book. So for me a very worth while question. Thanks everyone
Re^3: The date last monday
by johngg (Canon) on Oct 03, 2006 at 11:19 UTC
    Sorry I missed the drift of your OP. No, you are not asking too much at all. You need to transform the array slice using a map, like this

    perl -e 'printf qq{%4d%02d%02d\n},map{$_->[0]+1900,$_->[1]+1,$_->[2]}[ +((localtime(time-((1-(localtime(time))[6])* -86400)))[5,4,3])];'

    Note that the slice is passed into the map as an anonymus list.

    Cheers,

    JohnGG

      Thanks for the help, I finally understand map the missing link for me.

      Glad I felt like a challange this morning.
      Note that the slice is passed into the map as an anonymus list.

      <pedant>You mean an anonymous array.<pedant>

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        That's always confused me, thanks for pointing me at the relevant document. I had the mistaken idea that the distinction was whether the array held similar things like (1, 3, 4) or the list held different sorts of thing like ("fred", 27, "male"). By that standard calling it an anonymous list was still wrong, doh!

        Cheers,

        JohnGG

Log In?
Username:
Password:

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

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

    No recent polls found