Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Using "negative" characters with the range operator.

by Bowlslaw (Acolyte)
on Mar 10, 2017 at 17:45 UTC ( [id://1184206]=perlquestion: print w/replies, xml ) Need Help??

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

Hello! I have just discovered that you can use the range operator to print letters in alphabetical order(is it unicode order?). However, I have noticed something unusual about the behaviour of the range operator with regard to "negative" characters. For example:
my @array = 'a' .. -'a'; print @array; OUTPUT: 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 aa ab ac ad ae af +ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc +bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz. +..
And this pattern continues for each letter c, then a-z, etc. What is happening?

Replies are listed 'Best First'.
Re: Using "negative" characters with the range operator.
by choroba (Cardinal) on Mar 10, 2017 at 17:51 UTC
    See perlop, in particular the behaviour of the range operator in list context. Especially, the following paragraph:
    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.

    -'a' is the same as '-a' (see Unary "-" under Symbolic Unary Operators in perlop again) which for the range operator is the same as '*a', '%a', '^a' etc.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Using "negative" characters with the range operator.
by vrk (Chaplain) on Mar 12, 2017 at 16:25 UTC

    To answer the alphabetical order part of the question, it is alphabetical but only when you use ASCII letters. The range operator .. (in list context) is only able to increment strings matching /^[a-zA-Z]*[0-9]*\z/. If the start and end strings are composed of plain ASCII letters, you do get alphabetical order because the letters are arranged in alphabetical order in the ASCII code map. For example, A is followed by B because the integer value of B is 1 + the integer value of A.

    But the range operator doesn't work with Unicode codepoints. Besides, Unicode codepoints often aren't ordered alphabetically in any script, so you wouldn't get a sorted (collated) sequence even if it did.

      G'day vrk,

      "Besides, Unicode codepoints often aren't ordered alphabetically in any script, so you wouldn't get a sorted (collated) sequence even if it did."

      [Note: There's no intended pedantry here; however, as I understand your statement, I believe you mean "characters", not "codepoints". On that basis, I don't disagree with your statement, at all. The distinction is important for the remainder of my response.]

      The builtin module Unicode::Collate can be used for sorting Unicode characters.

      $ perl -E 'say for sort qw{z é a}' a z é $ perl -MUnicode::Collate -E 'say for Unicode::Collate::->new->sort(qw +{z é a})' a é z

      The code points are numerical values: a numerical sort is required for these.

      $ perl -E 'say for sort map { ord } qw{z é a}' 122 195 97 $ perl -E 'say for sort { $a <=> $b } map { ord } qw{z é a}' 97 122 195

      Code points are often presented as hexidecimal strings (that may have a leading "U+"). When dealing with these, it can be useful to first convert them to some canonical format. As the code point range is 0 .. 0x10ffff, an sprintf format including "%06x" or "%06X" handles all cases.

      $ perl -E 'say sprintf "U+%06X", $_ for map { ord } qw{z é a}' U+00007A U+0000C3 U+000061

      — Ken

        Yes indeed! Thanks for the clarification.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-03-28 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found