El mié., 26 sept. 2018 a las 8:27, Todd Chester (<toddandma...@zoho.com <mailto:toddandma...@zoho.com>>) escribió:



     >> El mié., 26 sept. 2018 a las 6:12, Todd Chester
    (<toddandma...@zoho.com <mailto:toddandma...@zoho.com>
     >> <mailto:toddandma...@zoho.com <mailto:toddandma...@zoho.com>>>)
    escribió:
     >>
     >>     Hi All,
     >>
     >> https://docs.perl6.org/routine/words
     >>     is no help
     >>
     >>     Not to ask too obvious a question, but why does words use a []
     >>     instead of a () ?

    On 09/25/2018 10:38 PM, JJ Merelo wrote:
     >
     >
     >
     >
     > Are you referring to a particular example? I can't find it.

    yes the ones in the original post.

     >
     > At any rate, $whatever.words[4] is the same as $whatever.words()[4].
     > That method needs no arguments, so it makes do without
    parentheses. When
     > it uses it, it's to limit the number of words returned.
     >
     > Cheers
     >
     > JJ


    I am confused as to when to use [] and when to use () with a method.

    The only thing can think of is that [] allows me to do thing like
         [4]
         [4,6]
         [4..6]
    Where I can't do it with ()

    But why is (4) different than [4]?

    And
    https://docs.perl6.org/routine/words#class_Str
         (Str) routine words

           multi sub    words(Str:D $input, $limit = Inf --> Positional)
           multi method words(Str:D $input: $limit = Inf --> Positional)

    Also is `Str:D $input:` written correctly?  I have only seen it as
    `Str:D:` before.


On 09/25/2018 11:29 PM, JJ Merelo wrote:
() is to pass arguments, [] is for array indexing. In your example, words(4) calls words with the number 4. words[4] would do words()[4], that is, words with no argument, that returns array, array indexed by 4


Something is wrong...

     $ p6 'say "1a 2b 3c 4c 5c".words("c");'
     Cannot convert string to number: base-10 number must begin with
     valid digits or '.' in '⏏c' (indicated by ⏏)
     in block <unit> at -e line 1

     $ p6 'say "1a 2b 3c 4c 5c".words(c);'
     ===SORRY!=== Error while compiling -e
     Undeclared routine:
     c used at line 1

     $ p6 'say "1a 2b 3c 4c 5c".words(3);'
     (1a 2b 3c)

     $ p6 'say "5a 4b 3c 2c 1c".words(4);'
     (5a 4b 3c 2c)


Boy the docs really struck out on explaining this!

How about my `Str:D $input:` question?

Reply via email to