sroux has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Here's a question I'd like to solve in Perl :

Any tips welcome for changing this formating (generation ordered http://download.oracle.com/docs/cd/E12825_01/epm.111/esb_dbag/dotdimb.htm#dotdimb1060220) :

gen2    gen3    gen4    gen5    gen6
A    B    C    D    E

into this one (recursively ordered http://download.oracle.com/docs/cd/E12825_01/epm.111/esb_dbag/dotdimb.htm#dotdimb1060462) :

Parent    Child
    A
A    B
B    C
C    D

My question/problem is how do I "navigate" between tab separated columns using Perl, how getting dynamically next or previous columns ... hmm array is the key it seems but then how to permute making it recursive!

Many thanks for guidance.

SRx
 

  • Comment on converting generation format to parent child format (recursive)

Replies are listed 'Best First'.
Re: converting generation format to parent child format (recursive)
by Util (Priest) on Apr 13, 2011 at 18:19 UTC

    I see no permutation or recursion in your example; In what way would this code be insufficient? :

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; my @fields = split /\s+/; # Change to /\t/ for production? # You can walk the array like this: print "\t$fields[0]\n"; for my $i ( 1 .. $#fields ) { print "$fields[$i-1]\t$fields[$i]\n"; } # Or like this: # my $last_field = ''; # for my $field (@fields) { # print "$last_field\t$field\n"; # $last_field = $field; # } print "\n"; } __DATA__ A B C D E Foo Bar Baz Senior Junior TheThird
    Output:
    A A B B C C D D E Foo Foo Bar Bar Baz Senior Senior Junior Junior TheThird