manav_gupta has asked for the wisdom of the Perl Monks concerning the following question:
I want to read this file and build a hash. I have the following code:200326951|_|rel_Access1|_|200315482|_| 200326951|_|rel_Access1|_|200315786|_| 200326951|_|rel_Access2|_|200315482|_| 200326951|_|rel_Access2|_|200315786|_|
However, that gives me a hash like so:my %hash; open(CMD1, "< test.txt"); while (<CMD1>) { my @elts = split(/\|_\|/, $_, -1); my $p = \\%hash; $p = \( ${$p}->{$_} ) for @elts; } use Data::Dumper; print Dumper(\%hash);
What can I do to get a hash like the following:$VAR1 = { '200326951' => { 'rel_Access2' => { '200315482' => { '' => undef }, '200315786' => undef }, 'rel_Access1' => { '200315482' => { '' => undef }, '200315786' => { '' => undef } } } };
Basically, I'm looking to discard duplicate values. This is a slightly tricky example, since in this one, the second line gets discarded because the second field (rel_Access1) was already present in the first line, and the third line gets discarded because the third field (200315482) was present in the result..$VAR1 = { '200326951' => { 'rel_Access2' => '200315786' }, 'rel_Access1' => '200315482' } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash from CSV-like structure
by FunkyMonk (Bishop) on Jan 04, 2008 at 20:40 UTC | |
by manav_gupta (Acolyte) on Jan 05, 2008 at 11:38 UTC | |
|
Re: hash from CSV-like structure
by jZed (Prior) on Jan 04, 2008 at 20:56 UTC | |
by manav_gupta (Acolyte) on Jan 05, 2008 at 11:51 UTC | |
by jZed (Prior) on Jan 05, 2008 at 19:06 UTC | |
by manav_gupta (Acolyte) on Jan 05, 2008 at 14:17 UTC | |
|
Re: hash from CSV-like structure
by naChoZ (Curate) on Jan 05, 2008 at 00:31 UTC | |
|
Re: hash from CSV-like structure
by doom (Deacon) on Jan 04, 2008 at 22:59 UTC | |
by jZed (Prior) on Jan 05, 2008 at 07:29 UTC |