Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

What is a stringwise operator?

by blahblah (Friar)
on Feb 25, 2008 at 05:12 UTC ( [id://669930]=perlquestion: print w/replies, xml ) Need Help??

blahblah has asked for the wisdom of the Perl Monks concerning the following question:

I recently ran into the following code snippet:

if ($releasedate lt '20070305') { ... }

In seeking to understand why the original coder used the lt operator instead of the < operator I went to perlop and found this:
Binary "lt" returns true if the left argument is stringwise less than +the right argument.

What does that mean? I can't find information explaining what "stringwise" means on perlmonks Super Search or google. Specifically, how is a string less than another string? Why would the original coder choose that operator over the numeric one?

Thanks.

Robert Browning on being asked what some line in one of his poems meant said
'When I wrote it only God and Robert Browning knew. Now only God knows.'

Replies are listed 'Best First'.
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.

      So that 'A' is less than 'B', both of which are less than 'a'.

      ...unless use locale is in effect.

        Erm, yes. Which is why I parenthetically remarked:

        (or possibly other character set order depending on the locale)

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

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

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!

      It works, so long as each element is 0 padded, and you're using consistent delimiters between the two items to be compared. The following won't work:

      '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.

        ISO 8601 Date and Time Specification Format
        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 ]

Re: What is a stringwise operator?
by ambrus (Abbot) on Feb 25, 2008 at 10:16 UTC

    Stringwise means that 0 < 1 < 10 < 11 < 12 < 13 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9.

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". ^.^

      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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found