in reply to Creating a Multi Level Hash from CSV

Please forgive me if I ask this, but what are you trying to accomplish?
Why do you think that you need the data structure that you are asking for?
How does the rest of the code use this?
I suspect that what you are asking for is not what you really need.
We call this an X-Y problem.
Please enlighten me.

Perl is amazing. If you "back up" just a bit to explain a bit more about the application, my suspicion is that you will get some ideas that you haven't even considered.

You give:

info01,info02,info03... info11,info12,info13..
And then:
$VAR1 = { info01 => { info02 => { info03 => { info02 => { info12 => { info13 => {
What happened to info11? Why does info02 become a higher level hash key?

I re-read your problem statement. Sounds like you have some of what I would call aliases. Perhaps "Cathy Smith" is the same person as "Cathy Smith Jones" or whatever. I am not sure what is going on.

Replies are listed 'Best First'.
Re^2: Creating a Multi Level Hash from CSV
by Fletch (Chancellor) on May 13, 2021 at 01:47 UTC

    OP definitely needs to elaborate for a better answer, but I've seen this kind of CSV-ish structure used once or twice to define something like a tree structure (not that it's a great idea, more there was a CSV hammer and hence . . .).

    Edit: Just to be slightly more concrete, they had something like this YAML

    --- Top A: - A1 - A2: - A2.1 - A2.2 Top B: - B1: - B1.1 - B1.2

    And they represented as something like:

    Top A, Top A,A1, Top A,A2, Top A,A2,A2.1 Top A,A2,A2.2 Top B, Top B,B1 Top B,B1,B1.1 Top B,B1,B1.2

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Creating a Multi Level Hash from CSV
by workInProgress12 (Novice) on May 13, 2021 at 17:15 UTC
    I'm really sorry about the confusion, fixed the sample output now. I'm supposed to print the CSV file separated with | as the end goal, which I realize could just be done with a simple split and join statement (which I was able to do) but was asked specifically to do with multi level hash, gotta do what the boss asks I guess.
      ... fixed the sample output ...

      Further to Marshall's post:

      ... it is good form to preserve the original version ...
      Please see How do I change/delete my post? The bottom line: Please do not destroy context.


      Give a man a fish:  <%-{-{-{-<

      I see your updates. BTW, when you update your post, it is good form to preserve the original version so later readers can figure out what is going on. You can "hide" say a previous section with <readme> <readmore>tags. Note: I made a typo and works out to be a demo of method of updating a post...the <strike> tags!

      I am still confused by this: Important to note that there are repeated values, which is the case for the provided sample output, (ie info 01 = info11, info02 = info12, etc.), but this is not always the case.. As you can see there have been multiple interpretations of your problem statement. My interpretation is shown below which allows for duplicated values in the CSV line(s).

      In general, the more you tell us about your problem, the more helpful the Monks can be. I didn't understand any more about what you are actually doing with this data structure. You may find that is a very awkward thing to work with.

      I would be curious to know how close my "crystal ball" got.

      use strict; use warnings; use Data::Dumper; my %hash; foreach my $line (<DATA>) #simulated simple CSV file { my @cols = (split /,|\n/,$line); my $href = \%hash; while (my $col = shift @cols) { $href->{$col} = {} unless (exists ($href->{$col}) and keys %{$href->{$col}} ); $href = $href->{$col}; } } print Dumper \%hash; =OUTPUT: $VAR1 = { 'x' => { '5' => { '6' => {} } }, 'a' => { 'x' => { 'e' => {}, 'm' => {}, 'c' => {} }, 'b' => { 'c' => {} }, 'y' => { 'h' => {} } } }; =cut __DATA__ a,x,c a,x,e a,y,h a,x,m a,b,c x,5,6
        You can "hide" say a previous section with <readme> tags.

        Maybe you wanted to say <readmore>?

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$