Why 'z'..'a' is not empty?

Discussion in 'Perl Misc' started by Peng Yu, Dec 6, 2012.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    The following code shows that 'z'..'a' is not empty. But perlop says
    the following. So it should return empty list. Does anybody know why
    'z'..'a' is not empty?

    "In list context, it returns a list of values counting (up by ones)
    from the left value to the right value. If the left value is greater
    than the right value then it returns the empty list."

    ~/linux/test/perl/man/perlop/dot_dot/list_context$ cat main.pl
    #!/usr/bin/env perl

    use warnings;

    print join(',', 1..10), "\n";
    print join(',', 10..1), "\n";
    print join(',', 'a'..'z'), "\n";
    print join(',', 'z'..'a'), "\n";

    ~/linux/test/perl/man/perlop/dot_dot/list_context$ ./main.pl
    1,2,3,4,5,6,7,8,9,10

    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
    z
    ~/linux/test/perl/man/perlop/dot_dot/list_context$ perl --version

    This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-
    thread-multi-2level
    (with 1 registered patch, see perl -V for more detail)

    Copyright 1987-2011, Larry Wall

    Binary build 1402 [295342] provided by ActiveState http://www.ActiveState.com
    Built Oct 7 2011 15:58:41

    Perl may be copied only under the terms of either the Artistic License
    or the
    GNU General Public License, which may be found in the Perl 5 source
    kit.

    Complete documentation for Perl, including FAQ lists, should be found
    on
    this system using "man perl" or "perldoc perl". If you have access to
    the
    Internet, point your browser at http://www.perl.org/, the Perl Home
    Page.
    Peng Yu, Dec 6, 2012
    #1
    1. Advertising

  2. Peng Yu

    C.DeRykus Guest

    On Thursday, December 6, 2012 8:33:28 AM UTC-8, Ben Morrow wrote:
    > Quoth Peng Yu <>:
    >
    > >

    >
    > > The following code shows that 'z'..'a' is not empty. But perlop says

    >
    > > the following. So it should return empty list. Does anybody know why

    >
    > > 'z'..'a' is not empty?

    >
    >
    >
    > This happens because of the way .. works on non-numeric values. It uses
    >
    > the properties of ++ on strings, which means that "z" will roll over to
    >
    > "aa" rather than to "{", but that also means that it can't compare the
    >
    > endpoints before starting because that would make "z".."ab" return the
    >
    > empty list.
    >
    >


    And to expand on this, IIUC, .. gets really
    twisty if there's magical ++ on strings:

    perl -lE 'say join(",", "m" .. "a")'
    m,n,o,p,q,r,s,t,u,v,w,x,y,z

    Similarly:
    perl -lE 'say join(",", "y" .. "a")'
    y,z


    Intuitively, you might think just as with
    numeric operands, this should yield the empty list since "m" (or "y" in the latter case) is "greater" than "a" and But, since they're strings, it'll ++ until "z" falls over the edge
    to "aa" which is longer than "a".


    from perlop:

    If the final value specified is not in the
    sequence that the magical increment would
    produce, the sequence goes until the next
    value would be longer than the final value
    specified.


    So, Peng's 'z' to 'a' example ++'s to 'aa'
    which it's longer than 'a' then stops which
    leaves only the initial 'z' as output.


    --
    Charles DeRykus
    C.DeRykus, Dec 7, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lukas
    Replies:
    3
    Views:
    789
    spiff
    Nov 10, 2005
  2. ButlerDJIAM
    Replies:
    0
    Views:
    546
    ButlerDJIAM
    Nov 9, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  5. Skybuck Flying
    Replies:
    16
    Views:
    662
    tragomaskhalos
    Aug 25, 2007
Loading...

Share This Page