Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using split with localtime 1

Status
Not open for further replies.

goBoating

Programmer
Feb 8, 2000
1,606
US
A curiosity of sorts......<br>Does anyone know of documentation about how 'split' relates to 'localtime'?&nbsp;&nbsp;It appearently is related, somehow.&nbsp;&nbsp;An illustration using version 5.004_04 on Solaris.<br>This code:<br><br>#!/usr/local/bin/perl<br><br>@timeVars = localtime(time);<br>print &quot;Straight from localtime\n&quot;;<br>print &quot;@timeVars&quot;;<br>print &quot;\n-----------------\n&quot;;<br><br><br>$when2 = localtime(time);<br>@vars = split(/ /,$when2);<br>print &quot;Date vars after use of split\n&quot;;<br>print &quot;@vars&quot;;<br>print &quot;\nyear - $vars[4]\n&quot;;<br><br>#end of code chunk<br><br>produces this text:<br><br>Straight from localtime<br>50 38 9 27 3 100 4 117 1<br>-----------------<br>Date vars after use of split<br>Thu Apr 27 09:38:50 2000<br>year - 2000<br><br><br>The use of 'split' magically converts the array '@vars' to date chunks.<br><br>Question:&nbsp;&nbsp;How does 'split' know it is operating on the result of a localtime call?<br>
 
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 &quot;/ /&quot; so the variable<br><br><FONT FACE=monospace><b>$x= &quot;The Cat Sat On The Mat&quot;;</font></b><br><br>could be split into an array like this<br><br><FONT FACE=monospace><b>@WhatTheCatDid = split(/ /,$x);</font></b><br><br>then <FONT FACE=monospace><b>@WhatTheCatDid[0]</font></b> would contain &quot;The&quot;, <FONT FACE=monospace><b>@WhatTheCatDid[1]</font></b> would contain &quot;Cat&quot; and so on.<br><br>Perhaps the &quot;Apr&quot; value is a result of interpreting the output of localtime in a string context - bit surprising though.<br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
'split' must have some <i>idea</i> where the array of numbers was created.&nbsp;&nbsp;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.&nbsp;&nbsp;There must be an infinite number of situations which could produce a list of numbers similar to the output of 'localtime'.&nbsp;&nbsp;I don't really need an answer.&nbsp;&nbsp;I just thought this was peculiar.&nbsp;&nbsp;Also, it is a kind of handy y2k fix.&nbsp;&nbsp;It <i>magically</i> transforms years since 1900 into the current year.&nbsp;&nbsp;Or, does it?
 
I've been using code like this - so I'm as mystified as you, very wierd.<br><br><FONT FACE=monospace><b><br>my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdt)=localtime(time);<br>$year+=1900;<br></font></b><br><br>The variable <FONT FACE=monospace><b>$year</font></b> is set to 2000 after this.<br><br>I take back what I said. Split must know about the return format of the <FONT FACE=monospace><b>localtime(time)</font></b> function - must be a hard coded thing.<br><br>Annoying really, had I not seen your post I would have written code that manipulated <FONT FACE=monospace><b>@vars</font></b> in the format returned by <FONT FACE=monospace><b>localtime(time)</font></b> - 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 <FONT FACE=monospace><b>localtime(time)</font></b><br><br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>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.
 
From the perl documentation for <FONT FACE=monospace><b>localtime()</font></b><br><br><FONT FACE=monospace><br>In scalar context, returns the ctime(3) value:<br><br><b>$now_string = localtime;</b>&nbsp;&nbsp;# e.g., &quot;Thu Oct 13 04:54:34 1994&quot;<br></font><br><br>This still doesn't answer the question I know, but it does give a hint - that <FONT FACE=monospace><b>localtime()</font></b> thinks it was evaluated in a scalar context, hmmmm or maybe that <FONT FACE=monospace><b>split()</font></b> just <i>thought</i> <FONT FACE=monospace><b>localtime()</font></b> was evaluated in a scalar context.<br><br>Too difficult, hurting my head now.<br><br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>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.
 
Mike,<br><br>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 &quot;$timeVars[5] + 1900&quot;.) When localtime is feeding its output to a scalar it sends a string similar to the default output of the unix command `date`.<br><br>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.<br><br>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.<br><br>- Kai.<br>
 
a little follow up......<br>if anyone uses this syntax to play with dates, <br>be aware that a single digit day value will be preceded with 2 spaces, not one.&nbsp;&nbsp;Therefore, splitting on spaces will get you the wrong array elements.<br><br>using......<br>$when2 =~ s/ +/ /gs; # replace multiple spaces with one space<br><br>...... will fix it.<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top