Re: What is a stringwise operator?
by Fletch (Bishop) on Feb 25, 2008 at 05:26 UTC
|
Stringwise means comparing according to the ASCII value order (or possibly other character set order depending on the locale). So that 'A' is less than 'B', both of which are less than 'a'. Strictly speaking a string comparison wasn't necessary in this case because YYYYMMDD dates sort naturally when used as numbers, but they may have had some other reason (perhaps there's data "INVALID" which they want to sort as a date in the far future . . .).
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] |
|
| [reply] [d/l] |
|
| [reply] |
Re: What is a stringwise operator?
by John M. Dlugosz (Monsignor) on Feb 25, 2008 at 06:14 UTC
|
String-like means alphabetical order, like a phone book.
The date you show is logically a string, and was probably read in that way. It doesn't make sense to multiply it by 2, or add 50, for example. A numeric sort might work, but it might also overflow the precision, and it would do funny things if there is a space instead of a 0, or contains other strings.
—John | [reply] |
Re: What is a stringwise operator?
by mattk (Pilgrim) on Feb 25, 2008 at 11:24 UTC
|
I feel kind of stupid for not realising this would work sooner. It's so damn cool, and it eats up delimiters and (some) missing stuff just fine:
perl> '2004-09-01 17:31:45' lt '2009-05-01 19:41:31'
-> 1;
perl> '2010-09-01 17:31:45' gt '2009-05-01 19:41:31'
-> 1;
perl> '2010-09-01' lt '2009-05-01 19:41:31'
-> '';
perl> '2010-09-01' gt '2009-05-01 19:41:31'
-> 1;
perl> '2010-09-01' lt '2009-05-01 19:41:31'
-> '';
No need to bust out DateTime, or even sort+split!
| [reply] [d/l] |
|
'2004-9-1' lt '2004-10-1'
'2004/09/01' lt '2004-09-01'
I typically get around the second issue by stripping out all non-digits, and then comparing. The first one I haven't found a simple one-line solution for, as I get stuff that's both delimited and run together. If it was always delimited:
sub normalize_date { return join( '', map { sprintf( '%02i', $_ ) } sp
+lit (/\D+/, $_[0]) ) }
sub compare_dates { return normalize_date($_[0]) cmp normalize_date($_
+[1]) }
hmm ... I guess this _would_ work for me, as the non-delimited stuff I get is always 0 padded.
| [reply] [d/l] [select] |
|
2007-10-05 11:34:22
Dates in the format above are inspired by the ISO Date and Time Specification Format, ISO 8601 for short. (Often, the Zulu is omitted, a minor faux-pas in ISO terms.) The benefits are obvious: since the digits are already in most-to-least significant order, the strings sort naturally in chronological order; also, by using standard separators the included or omitted fields can be parsed with confidence.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] |
|
Re: What is a stringwise operator?
by ambrus (Abbot) on Feb 25, 2008 at 10:16 UTC
|
| [reply] |
Re: What is a stringwise operator?
by ForgotPasswordAgain (Priest) on Feb 25, 2008 at 09:21 UTC
|
What does the suffix "-wise" mean in general, for example in "clockwise" or "lengthwise"? Clockwise: in the direction of a clock, the way of a clock. Lengthwise: in the direction of the length, the way of the length (of something). Otherwise: in an other way, a way other (than expected).
I guess "-wise" (ways) is related to "-wards", though "-wards" is more directional (in terms of spatial relations) than "-ways" (which is more the manner, or way, in which something is done). Towards, inwards, outwards; and you wouldn't really say "stringwards", I don't think.
Anyways.... that's a Perlmonkwise explanation of "stringwise". ^.^
| [reply] |
|
Clockwise: in the direction of a clock
I perceive that like myself you are older than 35. Children of the Digital Age aren't generally taught how to read analog clocks.
(My wife is a high school physics teacher, and always gets grief from her students when a lab involves an analog timer.)
| [reply] |