[Chicago-talk] get array element from a function return

Jim Thomason jim at jimandkoka.com
Fri Dec 3 12:09:39 PST 2010


To start with, you can just quote your list to get rid of your
punctuation marks:

perl -le 'print [qw(Sun Mon Tue Wed Thu Fri Sat)]->[[localtime(time)]->[6]]'

And then drop the unnecessary refs and derefs:

perl -le 'print qw(Sun Mon Tue Wed Thu Fri Sat)[(localtime)[6]]'

You could also use the fact that localtime in a scalar context returns
the ctime value, which'll have the day as the first 3 characters.

perl -le 'print substr(scalar(localtime),0,3)'

But there may be some portability issues across different locales or
languages...

-Jim....

On Fri, Dec 3, 2010 at 1:59 PM, tiger peng <tigerpeng2001 at yahoo.com> wrote:
> Hi,
> To get the weekday name of current day in Perl, I am using some thing like
> below. I do not like so many punctuation marks. Can it be simpler?
>
> perl -le 'print [("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
> "Sat")]->[[localtime(time)]->[6]]'
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>


More information about the Chicago-talk mailing list