6

I'm not totally sure if this is a bug, a quirk, or simply a bad practice, but this is what happens:

[0] > my @nope = [["a","b"]]; @nope.push: ["b","c"]
[a b [b c]]
[1] > my @yipee = []; @yipee.push: [["a","b"]]; @yipee.push: ["b","c"]
[[a b] [b c]]

So if you inialize an array with a list of lists, it gets Slipped, forcing to use 2 steps to initalize it. Is there something I'm missing here?

1
  • 1
    In the first example does the outer set of square brackets essentially 'de-containerize' the inner bracket? So that when you push ["b","c"] you get [a b [b c]] ? Commented Oct 16, 2024 at 10:03

1 Answer 1

5

Note that for the initialization of arrays, you don't need the outer pair of []. So your first example can be written as:

my @nope = ["a","b"];

Which may make the issue clearer to you: it's the single argument rule at work (which BTW allows you to say for @a { }). So either you need to make it look like more arguments by adding a comma (which makes it a List):

my @a = ["a","b"],;
dd @a;  # [["a", "b"],]

or you need to itemize the first array (as items never get implicitely flattened):

my @b = $["a","b"];
dd @b;  # [["a", "b"],]
Sign up to request clarification or add additional context in comments.

2 Comments

If I understood this directly, double square brackets are, in this context, understood as single square brackets. A case of not DWIM, maybe?
You could use double square brackets, but you'd need to make the inner a List by adding a comma, so my @a = [["a", "b"],];. But really, the outer square brackets are superstitious :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.