×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

using split with localtime

using split with localtime

using split with localtime

(OP)
A curiosity of sorts......
Does anyone know of documentation about how 'split' relates to 'localtime'?  It appearently is related, somehow.  An illustration using version 5.004_04 on Solaris.
This code:

#!/usr/local/bin/perl

@timeVars = localtime(time);
print "Straight from localtime\n";
print "@timeVars";
print "\n-----------------\n";


$when2 = localtime(time);
@vars = split(/ /,$when2);
print "Date vars after use of split\n";
print "@vars";
print "\nyear - $vars[4]\n";

#end of code chunk

produces this text:

Straight from localtime
50 38 9 27 3 100 4 117 1
-----------------
Date vars after use of split
Thu Apr 27 09:38:50 2000
year - 2000


The use of 'split' magically converts the array '@vars' to date chunks.

Question:  How does 'split' know it is operating on the result of a localtime call?

RE: using split with localtime

Split doesn't know. It just splits a string into a list of variables (in this case stuffed into an array) based on that pattern matching first argument "/ /" so the variable

$x= "The Cat Sat On The Mat";

could be split into an array like this

@WhatTheCatDid = split(/ /,$x);

then @WhatTheCatDid[0] would contain "The", @WhatTheCatDid[1] would contain "Cat" and so on.

Perhaps the "Apr" value is a result of interpreting the output of localtime in a string context - bit surprising though.

Mike
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: using split with localtime

(OP)
'split' must have some idea where the array of numbers was created.  It would seem to me to be very dangerous for 'split' to arbitrarily decide to interpret an array element differently as a alpha or numeric.  There must be an infinite number of situations which could produce a list of numbers similar to the output of 'localtime'.  I don't really need an answer.  I just thought this was peculiar.  Also, it is a kind of handy y2k fix.  It magically transforms years since 1900 into the current year.  Or, does it?

RE: using split with localtime

I've been using code like this - so I'm as mystified as you, very wierd.


my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdt)=localtime(time);
$year+=1900;


The variable $year is set to 2000 after this.

I take back what I said. Split must know about the return format of the localtime(time) function - must be a hard coded thing.

Annoying really, had I not seen your post I would have written code that manipulated @vars in the format returned by localtime(time) - why on earth would you write it to do that? There must, as you say, be an infinite number of situations which could produce a list of numbers similar to the output of localtime(time)

Mike
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site
Please don't send me email questions without posting them as well. Post the question and send me a note saying "Have a look at so-and-so would you?" - that's fine.

RE: using split with localtime

From the perl documentation for localtime()


In scalar context, returns the ctime(3) value:

$now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"


This still doesn't answer the question I know, but it does give a hint - that localtime() thinks it was evaluated in a scalar context, hmmmm or maybe that split() just thought localtime() was evaluated in a scalar context.

Too difficult, hurting my head now.

Mike
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site
Please don't send me email questions without posting them as well. Post the question and send me a note saying "Have a look at so-and-so would you?" - that's fine.

RE: using split with localtime

Mike,

You are on the right track. Scalar vs array context is everything in this instance. When localtime() is feeding its output to an array it sends a list of numbers. One of those numbers is the number of years since 1900. (Thus the current year is "$timeVars[5] + 1900".) When localtime is feeding its output to a scalar it sends a string similar to the default output of the unix command `date`.

The array lets you manipulate the various components easily, since they are numbers. Also you need to start from here if you want to custom format a timestamp. If all you need is a generic timestamp then the scalar output it handy and easy.

split doesn't know and doesn't care that it is operating on the output from a localtime(). It just knows that it was fed a string and told to split it on spaces, so it does.

- Kai.

RE: using split with localtime

(OP)
a little follow up......
if anyone uses this syntax to play with dates,
be aware that a single digit day value will be preceded with 2 spaces, not one.  Therefore, splitting on spaces will get you the wrong array elements.

using......
$when2 =~ s/ +/ /gs; # replace multiple spaces with one space

...... will fix it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close