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.
| [reply] [d/l] [select] |
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. | [reply] |
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |