tree to array of arrays

G

George Mpouras

I want to build an array of arrays from a random tree data structure.
for example if d... means "directory" and f... "file"
then from the data

/d1/d7/d8
/d1/d2/f1
/d1/d2/f2
/f1
/f2
/d1/f1
/d2/f3
/d4/d6/f1

The following array must be created. Any idea is wellcome.

[d1,
[d7,
[d8
],
[d2,
[f1],
[f2],
],
[f1],
],
[f1],
[f2],
[d2,
[f3],
],
[d4,
[d6,
[f1]
]
]
 
G

George Mpouras

With hashes of hashes is straight forward, but it have to be array, thanks
Kiuhnm.
 
W

Willem

George Mpouras wrote:
) I want to build an array of arrays from a random tree data structure.
) for example if d... means "directory" and f... "file"
) then from the data
)
) /d1/d7/d8
) /d1/d2/f1
) /d1/d2/f2
) /f1
) /f2
) /d1/f1
) /d2/f3
) /d4/d6/f1

If you sort this, it will be much easier. Is the order important?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
G

George Mpouras

Yes and no, (for one pass order is important)
But if there is no other way, a initial sort is ok
 
R

Rainer Weikusat

George Mpouras said:
for me a random %treehash to $nested_array_of_arrays is not trivial
at all

A possible way to do this:

----------------
my %h = (
a => 1,
b => {
c => 1,
u => 1,
d => {
e => 1},
f => 1 },
);

sub to_a($)
{
my (@a, $v);

for (keys(%{$_[0]})) {
$v = $_[0]->{$_};
push(@a, ref($v) ? [$_, to_a($v)] :$_);
}

return \@a;
}

use Data::Dumper;

print Dumper(to_a(\%h));
------------------
 
N

ntua

that is excellent, thanks alot Rainer. I think now I can make the whole
think work.
first tree -> hash and then your solution
 
T

Tim McDaniel

Just keep in mind that hashes don't care about order. If order is
important to you, you'll need a tree of "pairs",

or use Tie::Hash::Indexed or Tie::IxHash, which appear to have a hash
interface yet care about order.
 
T

Tim McDaniel

Kiuhnm said:
Just keep in mind that hashes don't care about order. If order is
important to you, you'll need a tree of "pairs",

I replied:

or use Tie::Hash::Indexed or Tie::IxHash, which appear to have a
hash interface yet care about order.

tie is also slow compared to direct assignments

Direct assignments where you're coding a structure to hold the
contents of an ordered hash is going to be slow and erro-prone too.
 
G

George Mpouras

# Here is one solution to the problem
# If you can make it more compact, please it 'll be great



use strict;
use warnings;
use feature 'say';

my $hash={};
while(<DATA>) { chomp $_;
List_to_hash(split /\//, $_)
}

my $array = Hash_to_Array($hash);


sub List_to_hash
{
my $lastkey = pop @_;
my $Ref = $hash;

foreach (@_)
{
$Ref->{$_} = {} unless ref $Ref->{$_};
$Ref = $Ref->{$_}
}

$Ref->{$lastkey} = 1
}



sub Hash_to_Array
{
my (@Array, $v);

foreach (keys %{$_[0]})
{
$v = $_[0]->{$_};

if ( ref $v )
{
push @Array, [$_, Hash_to_Array($v)]
}
else
{
push @Array, $_
}
}
\@Array
}



#use Data::Dumper; print Dumper(\%hash); exit;
use Data::Dumper; print Dumper($array); exit;

__DATA__
d1/d7/d8
d1/d2/f1
d1/d2/f2
f1
f2
d1/f1
d2/f3
d4/d6/f1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top