Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: array assignment

by Anonymous Monk
on Nov 13, 2004 at 18:41 UTC ( [id://407619]=note: print w/replies, xml ) Need Help??


in reply to array assignment

Thank you guys, all the suggestions were very interesting. Esp. mapcar was - yes, cool!

Inspired with what I've learnt here I contribute with a variation

my ($dd, $mm, $yyyy) = map { $_->[0], $_->[1] + 1, $_->[2] + 1900 } ([ +(localtime)[3..5]]);
L

Replies are listed 'Best First'.
Re^2: array assignment
by hippo (Bishop) on Jun 23, 2022 at 09:16 UTC

    What you have there will work but I would not use map to iterate over a list with only one element like that. It's confusing (another programmer coming to this code will likely struggle to understand why it has been done this way) and probably inefficient.

    FWIW, these days we have Time::Piece in core so if you want to do this sort of date decomposition in 2022 that's what I would use and recommend:

    #!/usr/bin/env perl use strict; use warnings; use Time::Piece; my ($dd, $mm, $yyyy) = split /-/, localtime->dmy;

    This also helps save you from bugs if/when getting the maths wrong or using the wrong positions in the source array.


    🦛

Log In?
Username:
Password:

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

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

    No recent polls found