in reply to create a level csv

The following is much closer to producing your desired output. It does not produce it exactly because I don't understand some of the patterns in your sample output.

use strict; use warnings; my $folder_IN = $ARGV[0]; my $finaltreefile = "./final.CSV"; my $level = 0; my $rootfile = `ls $folder_IN/$level*.CSV`; chomp($rootfile); print "rootfile: $rootfile\n"; open( my $final, '>', $finaltreefile ) or die "$finaltreefile: $!"; treebuilder($rootfile, $level+1); close($final); exit(0); sub treebuilder { my $filename = shift; my $level = shift; my $nextlevel = $level + 1; print "File level $level : $filename\n"; my $PRESENTLEVELFILE; unless(open($PRESENTLEVELFILE, '<', $filename)) { warn "$filename: $!"; return; } my $leafline = <$PRESENTLEVELFILE>; if ( $filename eq $rootfile ) { print $final "0 $leafline"; } while ( my $leafline = <$PRESENTLEVELFILE> ) { print "Leggo $leafline\n"; chomp($leafline); print "next level=$nextlevel\n"; my @element = split( '§', $leafline ); print "Find $element[0]\n"; my $NEXTLEVELFILE = $folder_IN . "/" . $level . "_" . $element[0] . ".CSV"; print "Check if file exists: $NEXTLEVELFILE\n"; print $final "$level $leafline\n"; treebuilder($NEXTLEVELFILE, $nextlevel); } close($PRESENTLEVELFILE); }

I have made several changes from your program. One of the more significant changes was to use lexical variables for the file handles. As it was you were reusing the same filehandle in each recursive call to treebuilder. As a result, each call to open(CURRENTLEVELFILE) closed the currently open file, preventing the caller from carrying on reading from it. With lexicals for file handles, each recursive call to treebuilder() has its own filehandle.

Otherwise, I used Perl::Tidy to reformat the file and then simplified the structure, shortening individual blocks of code. These changes make the code easier to read and understand (to my tastes at least), and therefore easier to correct. There were, after all, few errors in your original code but it was difficult to find them before reorganizing.

This version produces the following output from your sample files:

0 440Z0220-932§other information 1 623Z5400-1§other information 1 623Z5400-1§other information 1 623Z5400-18other information 1 623Z5400-18§other information 1 623Z5400-3§other information 1 623Z5420-1301§other information 1 623Z5420-1307§other information 1 7010902H01§other information 1 7010900H01§other information 1 623Z5466-1013§other information 1 623Z5466-1015§other information 1 623Z5465-1039§other information 1 623Z5465-1041§other information 1 623Z5465-1001§other information 2 623Z5465-1§other information 2 623Z5465-11§other information 2 CA623Z5465-1001§other information 1 623Z5420-3301§other information 1 623Z5465-41§other information 1 623Z5462-1001§other information 1 623Z5462-1003§other information 2 623Z5468-1§Other information 2 623Z5468-9§Other information 2 623Z5468-7§Other information 2 623Z5468-5§Other information 2 CA623Z5462-1003§Other information 2 BACD40AC36B30§Other information 2 623Z5469-7§Other information 2 623Z5462-3§Other information 1 7010902H01§other information 1 623Z5420-3307§other information 1 B3-15838§other information 1 623Z5422-3307§other information

update: With the debug prints removed and a few more minor changes to tidy it up, the treebuilder sub is even easier to read:

sub treebuilder { my ($filename, $level) = @_; my $PRESENTLEVELFILE; unless(open($PRESENTLEVELFILE, '<', $filename)) { warn "$filename: $!"; return; } my $leafline = <$PRESENTLEVELFILE>; print $final "0 $leafline" if ( $filename eq $rootfile ); while ( my $leafline = <$PRESENTLEVELFILE> ) { print $final "$level $leafline"; my @element = split( '§', $leafline ); treebuilder("$folder_IN/${level}_$element[0].CSV",$level + 1); } close($PRESENTLEVELFILE); }

Replies are listed 'Best First'.
Re^2: create a level csv
by Paulux (Acolyte) on Apr 16, 2009 at 15:01 UTC
    Thanx a lot for the answer. i'd like to know what you don't understand. Please tell me and I'll try to explain it better.

      The following line - the first line in your root file - appears twice: once at level 0 and a second time at level 1.

      0 440Z0220-932§other information 1 440Z0220-932§other information

      Similarly the following line - the first line in one of your leaf files - appears twice: once at level 1 and a second time at level 2.

      1 623Z5462-1003§other information 2 623Z5462-1003§Other information

      But the following line - the first line in the other of your leaf files - appears only once. I don't understand why this line is different from the others.

      1 623Z5462-1001§other information

      The first line - at level 0 - seems odd to me.

      And it seems odd that any of the lines are duplicated.

        In the output file only the first line of the "root" file have to be written that is the level 0 of a structure. The next lines are the component that can link to the first line of the other files. This line doesn't be written in the output file. Infact in the previous example I have done a mistake.