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

Hi all,

My aim is to write a program that will display the parent-child data in an array. I have an input file called 1.txt. This file contain data such as 10,20,30,40 Foreach data, read from the input file append by .txt to get the sub file data for the next...

The relation is as follows:

1 -> 10, 20, 30, 40 10 -> 100, 101, 102 100 -> no data 101 -> 1011 1011 -> no data 102 -> no data 20 -> no data 30 -> 301, 302 301 -> no data 302 -> no data 40 -> 401, 402 401 -> 4011 4011 -> 40111 40111 -> no data 402 -> no data

(1,10,20,30,40,100,101,102,1011,301,302,401,402,4011,40111 .. are .txt files)

Need to start reading from 1.txt file and the resultant data from that should read subsequent files, finally all data should be stored in an array.

My program works having some problem to store all data... Could anyone please help me to solve this?

My code is given below:

#!/usr/bin/perl @ini_array = (); #input file open(F,"C:/test/1.txt") or die "Can't open 1.txt file"; while(<F>) { chomp; push(@ini_array,$_); } close F; @final_array; foreach $ab (@ini_array) { push(@final_array,$ab); &common($ab); } sub common() { $fl = shift; open(A,"C:/test/$fl.txt") or die "Can't open $fl"; @sublist = <A>; close A; $cnt = scalar @sublist; if ($cnt > 0) { subfl(\@sublist); } else { push(@final_array,@sublist); } } sub subfl() { $arfirst = shift; @ini_array1 = @$arfirst; foreach $ab1 (@ini_array1) { chomp($ab1); push(@final_array,$ab1); &common($ab1); } }
Thanks

Edit: g0n - code tags and formatting

Replies are listed 'Best First'.
Re: store parent child relation
by TedPride (Priest) on May 18, 2006 at 08:00 UTC
    This is tested and works, assuming the format is the same as what you said (just a single line of numbers separated by commas in each file). You'll need to change the path for opening files, though, and if this is for homework (which seems likely), you'll want to rewrite the code and make sure you understand all of it.
    use strict; use warnings; use Data::Dumper; my $tree = build(1); print Dumper($tree); my @arr = traverse($tree); print "@arr"; sub traverse { my $p = $_[0]; return ($p->{'key'}, map { traverse($_) } @{$p->{'branches'}}); } sub build { my (%node, $key, @branches, $handle); $key = $_[0]; $node{'key'} = $key; $node{'branches'} = \@branches; if (open ($handle, "$key.txt")) { chomp($_ = join '', <$handle>); close($handle); for (split /,/, $_) { push @branches, build($_); } } return \%node; }
      TedPride,
      Thanks a lot for excellent code and help.
Re: store parent child relation
by turo (Friar) on May 18, 2006 at 08:13 UTC

    I've made a wide exploration of the tree, without recursion, the result is akin the one you've shown on your post

    #!/usr/bin/perl @ini_array = getarr("1"); @final_array = (1); while (@ini_array) { @aux = (); push @final_array, @ini_array; map { push @aux, getarr($_) } @ini_array; @ini_array = @aux; } sub getarr { $fl = shift; @sublist=(); open(A,"$fl.txt") or die "Can't open $fl.txt"; while (<A>) { chomp; push @sublist, $_; } close A; return @sublist; } print "final: @final_array\n";
    hope that helps...

    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
      turo,
      Thanks a lot for your help.