Hi Marc,

here's a one-liner based on the classify method, which you may find to
be an interesting jumping-off-point, or centerpiece:

perl6 -e 'use JSON::Fast; my %foo =
lines()>>.trim-leading.classify(*.split(",").head(2)); say to-json %foo'
    A,1,garbage .....
    A,2,garbage .....
    A,2,garbage .....
    A,2,garbage .....
    A,3,garbage .....
    A,3,garbage .....
    A,3,garbage .....
    B,1,garbage .....
    B,2,garbage .....
    B,2,garbage .....
    B,2,garbage .....
    B,3,garbage .....
    B,3,garbage .....
    B,3,garbage .....
{
  "A": {
    "3": [
      "A,3,garbage .....",
      "A,3,garbage .....",
      "A,3,garbage ....."
    ],
    "1": [
      "A,1,garbage ....."
    ],
    "2": [
      "A,2,garbage .....",
      "A,2,garbage .....",
      "A,2,garbage ....."
    ]
  },
  "B": {
    "3": [
      "B,3,garbage .....",
      "B,3,garbage .....",
      "B,3,garbage ....."
    ],
    "1": [
      "B,1,garbage ....."
    ],
    "2": [
      "B,2,garbage .....",
      "B,2,garbage .....",
      "B,2,garbage ....."
    ]
  }
}

Things to note are: it makes hashes, so the order isn't retained. if
that's important, you could, for example, use/build a Hash that retains
its key insertion order and call .categorize-list on an instance of it.

Hope that helps, and Good Luck!
  - Timo

On 22/11/2019 16:06, Marc Chantreux wrote:
> hello,
>
> On Fri, Nov 22, 2019 at 03:07:28PM +0100, Patrick Spek via perl6-users wrote:
>> Could you post some input and expected output? That would make it
>> easier for me (and perhaps others) to see what exactly you're trying to
>> accomplish, in practical terms.
> sorry ... i'm so confortable with awk i haven't though about some are
> not. i sincerely apologize.
>
> Basically i want to figure out what are the sections, subsections of of
> a CSV file:
>
>     A,1,garbage .....
>     A,2,garbage .....
>     A,2,garbage .....
>     A,2,garbage .....
>     A,3,garbage .....
>     A,3,garbage .....
>     A,3,garbage .....
>     B,1,garbage .....
>     B,2,garbage .....
>     B,2,garbage .....
>     B,2,garbage .....
>     B,3,garbage .....
>     B,3,garbage .....
>     B,3,garbage .....
>
> becomes
>
>     A
>         1
>         2
>         3
>     B
>         1
>         2
>         3
>
> regards,
> marc

Reply via email to