6

If I understand correctly, when I assign values to an array of strings with < ... >, I should escape special symbols with \:

> my @array = < \\ a b>
[\ a b]
> my @array = < \< a b>
[< a b]
> my @array = < \<de\< a b>
[<de< a b]

Using backslashes is not always convenient, sometimes the code may become obscure.

Is there a better way to pass a list of strings containing special characters to an array?

Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40

1 Answers1

7

Use << >> instead of < >, and use single quotes inside:

> my @array = << '<de<' a b>>
[<de< a b]
Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
  • Strangely, it works even without single quotes! `my @array = << >` – Eugene Barsky Oct 29 '17 at 17:04
  • @EugeneBarsky Thank you for your SO posts exploring P6 regex and related features. :) If you would read the doc section [Word quoting with interpolation and quote protection: qqww](https://docs.perl6.org/language/quoting#Word_quoting_with_interpolation_and_quote_protection:_qqww) and give us feedback on it, that would be very helpful. In particular, does it explain to you why it works both with or without single quotes? – raiph Oct 29 '17 at 17:39
  • @raiph, thanks! I hope my numerous questions are not annoying (and as always, sorry for my English). P6 already helps me with my work, so most of my questions grow out of my attempts to do something that I need. I somehow missed the whole docs webpage about quoting, when searching for some info about `<>`. So I'll work it through and then write here my impressions. – Eugene Barsky Oct 29 '17 at 19:14
  • @raiph A very nice piece of documentation, and in my case I've decided to use `Qw` :) As for `qqww` producing the same result with my example, I suppose that's because ` – Eugene Barsky Oct 29 '17 at 20:48
  • 1
    @EugeneBarsky Imo the more highly specific work driven questions you ask the better. Maybe add something like "See also « » quotes and other quote forms." (suitably linked) to [the `<<>>` doc page](https://docs.perl6.org/routine/%7B%20%7D#%28Operators%29_postcircumfix_%7B_%7D)? Love that you liked the doc. :) Please consider dropping in for a few minutes and free virtual pizza during the Nov 4th [P6 squashathon](https://github.com/rakudo/rakudo/wiki/Monthly-Bug-Squash-Day) to say thanks to doccers about that doc. :) `Qw` makes total sense. And I believe you have it spot on about `qqww` too. :) – raiph Oct 30 '17 at 03:16
  • @raiph, thanks! You mean adding the link to my post here or to the corresponding docs webpage? When reading the docs, I sometimes lack code examples, but that's, I think, is because I'm a beginner and I'm not yet accustomed to the structure of the language and the docs. – Eugene Barsky Oct 30 '17 at 07:19
  • I'm not sure I understand how the 'squashathon' works (never took part in such things before). Is it necessary to previously register on the p6 IRC channel (to be honest, I've never used IRC either :) )? – Eugene Barsky Oct 30 '17 at 07:19
  • @EugeneBarsky I'm thinking about docs. Does change I suggested make sense? Maybe make results of search for `<>` include a link to the quoting page instead? – raiph Oct 30 '17 at 15:00
  • No registration is involved. I use kiwiirc. To try it now: [click here](https://kiwiirc.com/nextclient/); click `Add a Network` button; write `irc.freenode.org` in `Server` field; edit the `Nickname` field; click `Connect` button; write `#perl6` over 'Join new #channel' on left; hit Enter; write `hi` over 'Send a message' at the bottom; hit Enter. If/when folk are around and have time to reply they will. Now you're an irc user. :) – raiph Oct 30 '17 at 15:02
  • @raiph After I've missed it myself, I think any link to the webpage about types of quoting will be useful. :) Thanks, I'll try to use irc, though I'm not sure I understand what it is. :) – Eugene Barsky Oct 30 '17 at 17:35
  • @raiph It seemingly works, thanks! If I'm not mistaken, it looks like a group chat. :) What kinds of questions can one ask on this channel? – Eugene Barsky Oct 30 '17 at 17:42
  • @EugeneBarsky I see you said hi on [today's log](https://irclog.perlgeek.de/perl6/2017-10-30#i_15375916). My reply is [hopefully better late than never](https://irclog.perlgeek.de/perl6/2017-10-30#i_15377061)... – raiph Oct 30 '17 at 21:58
  • @raiph Thanks :) So if I understand correctly, I see all the messages on irclog.perlgeek.de and I can write via kiwiirc (there I don't see the messages while I'm out)? And the 2nd question is, where I can read the rules of behaving and the types of questions discussed via irc? – Eugene Barsky Oct 31 '17 at 07:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157924/discussion-between-raiph-and-eugene-barsky). – raiph Oct 31 '17 at 17:03