Indented text converted to arrays of arrays

Discussion in 'Perl Misc' started by Brad Baxter, Jun 29, 2004.

  1. Brad Baxter

    Brad Baxter Guest

    Greetings,

    I would like to take a table of indented text like the following:

    AAA
    BBB
    CCC
    DDD
    EEE
    FFF
    GGG

    .... and convert it into a nested array of arrays like the following:

    [
    [ 'AAA' => [
    'BBB',
    'CCC',
    ] ],
    [ 'DDD' => [
    [ 'EEE' => [
    'FFF',
    ] ],
    ] ],
    'GGG',
    ]

    That is, if an element $a is an array ref, then $a->[0] is the text for
    that level and $a->[1] is an array ref to the next level(s). Otherwise $a
    is the text for that level.

    The indenting is regular, the levels may be arbitrarily deep, and no
    double indenting is allowed, i.e., the following is disallowed:

    AAA
    BBB
    CCC
    DDD

    The solution I've come up with so far is to create the following literal
    string and eval it:

    [
    [ 'AAA' => [
    [ 'BBB' => [
    ] ],
    [ 'CCC' => [
    ] ],
    ] ],
    [ 'DDD' => [
    [ 'EEE' => [
    [ 'FFF' => [
    ] ],
    ] ],
    ] ],
    [ 'GGG' => [
    ] ],
    ]

    This obviously is not exactly what I describe above, but I can make it
    work. However, this solution seems rather draconian, and I feel certain
    there is a much simpler answer.

    Regards,

    Brad
    Brad Baxter, Jun 29, 2004
    #1
    1. Advertising

  2. Brad Baxter

    Tore Aursand Guest

    On Tue, 29 Jun 2004 17:37:50 -0400, Brad Baxter wrote:
    > I would like to take a table of indented text like the following:
    > [...]


    I needed help with something quite related to this, but I really don't
    know if the answers I got will help you.

    You can read the whole thread here:
    <http://tinyurl.com/2mb39>


    --
    Tore Aursand <>
    "Progress is made by lazy men looking for easier ways to do things."
    (Robert Heinlein)
    Tore Aursand, Jun 30, 2004
    #2
    1. Advertising

  3. Brad Baxter

    Brad Baxter Guest

    On Wed, 30 Jun 2004, Tore Aursand wrote:

    > On Tue, 29 Jun 2004 17:37:50 -0400, Brad Baxter wrote:
    > > I would like to take a table of indented text like the following:
    > > [...]

    >
    > I needed help with something quite related to this, but I really don't
    > know if the answers I got will help you.
    >
    > You can read the whole thread here:
    > <http://tinyurl.com/2mb39>


    Those answers weren't what I was after, but you inspired me to look
    further and I did find an answer here:

    http://groups.google.com/groups?selm=

    By mangling Ben's solution, I got just what I wanted.
    I'm a happy camper. :)

    Thanks!

    Brad


    #!/usr/bin/perl
    use strict;
    use warnings;

    use Data::Dumper;
    $Data::Dumper::Indent = 1;

    print Dumper make_array( <<_end_, " ", 4 );
    AAA
    BBB
    CCC
    DDD
    EEE
    FFF
    GGG
    _end_

    # make_array parameters:
    # $raw - string of indented text with newlines
    # $char - indent character (typically space or tab)
    # $num - number of $char's per indent level

    sub make_array {
    my( $raw, $char, $num ) = @_;

    my @cooked;
    my @a = split "\n", $raw;

    for my $i ( 0 .. $#a ) {
    my( $indent, $string ) = $a[ $i ] =~ /^($char*)(.*)/;
    my $len = length( $indent );
    my( $lookahead ) = $i == $#a ? '': $a[ $i+1 ] =~ /^($char*)/;
    $lookahead = length( $lookahead ) > $len;
    my $level = $len/$num;
    my $dref = \@cooked;
    $dref = $dref->[-1][-1] for 1 .. $level;
    push @$dref, $lookahead ? [$string,[]] : $string;
    }
    return \@cooked;

    } # end sub make_array

    __END__
    $VAR1 = [
    [
    'AAA',
    [
    'BBB',
    'CCC'
    ]
    ],
    [
    'DDD',
    [
    [
    'EEE',
    [
    'FFF'
    ]
    ]
    ]
    ],
    'GGG'
    ];
    Brad Baxter, Jun 30, 2004
    #3
    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. Martin
    Replies:
    1
    Views:
    5,371
    Scott M.
    Mar 4, 2004
  2. Replies:
    2
    Views:
    9,045
    Steve Holden
    Feb 28, 2008
  3. B Mills

    parsing indented plain text

    B Mills, Jan 31, 2007, in forum: Ruby
    Replies:
    1
    Views:
    263
    Luke Ivers
    Jan 31, 2007
  4. P

    Parsing indented text file

    P, Aug 10, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    182
    Mumia W.
    Aug 10, 2006
  5. Oscar Benjamin
    Replies:
    0
    Views:
    156
    Oscar Benjamin
    Sep 21, 2013
Loading...

Share This Page