tree to array of arrays

Discussion in 'Perl Misc' started by George Mpouras, Feb 23, 2012.

  1. 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]
    ]
    ]
     
    George Mpouras, Feb 23, 2012
    #1
    1. Advertising

  2. With hashes of hashes is straight forward, but it have to be array, thanks
    Kiuhnm.
     
    George Mpouras, Feb 24, 2012
    #2
    1. Advertising

  3. On Fri, 24 Feb 2012 16:01:38 +0200, George Mpouras wrote:

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


    Converting a HaH to a LoL is also rather trivial.

    HTH,
    M4
     
    Martijn Lievaart, Feb 24, 2012
    #3
  4. George Mpouras

    Willem Guest

    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
     
    Willem, Feb 24, 2012
    #4
  5. Yes and no, (for one pass order is important)
    But if there is no other way, a initial sort is ok
     
    George Mpouras, Feb 24, 2012
    #5
  6. for me a random %treehash to $nested_array_of_arrays is not trivial at all
     
    George Mpouras, Feb 24, 2012
    #6
  7. "George Mpouras" <> writes:
    > 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));
    ------------------
     
    Rainer Weikusat, Feb 24, 2012
    #7
  8. George Mpouras

    ntua Guest

    that is excellent, thanks alot Rainer. I think now I can make the whole
    think work.
    first tree -> hash and then your solution
     
    ntua, Feb 24, 2012
    #8
  9. George Mpouras

    Tim McDaniel Guest

    In article <4f47e896$0$1387$>,
    Kiuhnm <kiuhnm03.4t.yahoo.it> wrote:
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, Feb 24, 2012
    #9
  10. tie is also slow compared to direct assignments
     
    George Mpouras, Feb 24, 2012
    #10
  11. George Mpouras

    Tim McDaniel Guest

    Kiuhnm <kiuhnm03.4t.yahoo.it> wrote:
    >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.


    In article <ji8tq8$26m8$>,
    George Mpouras <> wrote:
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, Feb 24, 2012
    #11
  12. # 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
     
    George Mpouras, Feb 25, 2012
    #12
  13. George Mpouras

    Dr.Ruud Guest

    On 2012-02-25 22:31, George Mpouras wrote:

    > split /\//, $_


    You can write that as "split m{/}".
    Pretty hey?

    --
    Ruud
     
    Dr.Ruud, Feb 26, 2012
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,184
  2. Philipp
    Replies:
    21
    Views:
    1,160
    Philipp
    Jan 20, 2009
  3. Cec Tre
    Replies:
    3
    Views:
    199
    Robert Klemme
    Mar 19, 2010
  4. Allen Walker

    Merging two arrays -> array of arrays

    Allen Walker, May 21, 2010, in forum: Ruby
    Replies:
    6
    Views:
    174
    Jesús Gabriel y Galán
    May 21, 2010
  5. Hal Vaughan

    Arrays Of Arrays: Is it an Array or Scalar?

    Hal Vaughan, Feb 5, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    157
    Tore Aursand
    Feb 6, 2004
Loading...

Share This Page