Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Indented text converted to arrays of arrays

Reply
Thread Tools

Indented text converted to arrays of arrays

 
 
Brad Baxter
Guest
Posts: n/a
 
      06-29-2004
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
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      06-30-2004
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 <(E-Mail Removed)>
"Progress is made by lazy men looking for easier ways to do things."
(Robert Heinlein)
 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      06-30-2004
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...0earthlink.net

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:umper;
$Data:umper::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'
];
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: only length-1 arrays can be converted to Python scalars. nure123@gmail.com Python 2 02-28-2008 08:05 PM
parsing indented plain text B Mills Ruby 1 01-31-2007 05:33 PM
Parsing indented text file P Perl Misc 1 08-10-2006 08:41 AM
Writer required for indented XML output? Ian Pilcher Java 0 08-14-2005 03:19 PM
making indented XML Steve Maring Java 0 01-10-2004 03:13 AM



Advertisments