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

Hi to Everyone,
This is the first time that i write here. I have several files, this is the node:
filename: 0_IP_017.CSV
440Z0220-932§other information 623Z5400-1§other information 623Z5400-1§other information 623Z5400-18other information 623Z5400-18§other information 623Z5400-3§other information 623Z5420-1301§other information 623Z5420-1307§other information 7010902H01§other information 7010900H01§other information 623Z5466-1013§other information 623Z5466-1015§other information 623Z5465-1039§other information 623Z5465-1041§other information 623Z5465-1001§other information 623Z5420-3301§other information 623Z5465-41§other information 623Z5462-1001§other information 623Z5462-1003§other information 7010902H01§other information 623Z5420-3307§other information B3-15838§other information 623Z5422-3307§other information

the leafs:
filename: 1_623Z5462-1003.CSV
623Z5462-1003§Other information 623Z5468-1§Other information 623Z5468-9§Other information 623Z5468-7§Other information 623Z5468-5§Other information CA623Z5462-1003§Other information BACD40AC36B30§Other information 623Z5469-7§Other information 623Z5462-3§Other information

filename: 1_623Z5465-1001.CSV
623Z5465-1001§other information 623Z5465-1§other information 623Z5465-11§other information CA623Z5465-1001§other information

and I have to create a file like this:
0 440Z0220-932§other information 1 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 7010900H01other 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 1 623Z5420-3301§other information 1 623Z5465-41§other information 1 623Z5462-1001§other information 2 623Z5465-1§other information 2 623Z5465-11§other information 2 CA623Z5465-1001§other information 1 623Z5462-1003§other information 2 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

And here my code:
#!/usr/bin/perl $folder_IN=$ARGV[0]; $level=0; $finaltreefile="./final.CSV"; $rootfile=`ls $folder_IN/$level*.CSV`; print "rootfile: $rootfile\n"; &treebuilder($rootfile); sub treebuilder{ $PRESENTLEVELFILE=shift; print "File level $level : $PRESENTLEVELFILE\n"; $level++; open(PRESENTLEVELFILE); while ($leafline=<PRESENTLEVELFILE>){ print "Leggo $leafline\n"; chomp($leafline); if ($. == 1){ if($PRESENTLEVELFILE =~ /^0*/i){ open(FINAL,">$finaltreefile"); print FINAL "0 $leafline\n"; close(FINAL); } next; }else{ print "next level=$level\n"; @element=split('§',$leafline); chomp($element[0]); print "Find $element[0]\n"; $NEXTLEVELFILE=$folder_IN."/".$level."_".$element[0].".CSV"; chomp($NEXTLEVELFILE); print "Check if file exists: $NEXTLEVELFILE\n"; if (-e $NEXTLEVELFILE){ print "$NEXTLEVELFILE !esiste!\n"; &treebuilder($NEXTLEVELFILE); open(FINAL,">>$finaltreefile"); $level++; print FINAL "$level $leafline\n"; close FINAL; } print "$NEXTLEVELFILE ?not exists?\n"; open(FINAL,">>$finaltreefile"); print FINAL "$level $leafline\n"; close FINAL; } } }

executing my code I only can have this:
0 440Z0220-932§other information 1 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 1 623Z5420-3301§other information 1 623Z5465-41§other information 1 623Z5462-1001§other information 2 623Z5465-1§other information 2 623Z5465-11§other information 2 CA623Z5465-1001§other information

where I do a mistake?

Replies are listed 'Best First'.
Re: create a level csv
by ELISHEVA (Prior) on Apr 16, 2009 at 08:49 UTC

    Perhaps you could add an explanation of your goal, "in words"? The output listing you've provided shows us what the output looks like, but not what it means. It looks like you might be counting something, but at present the only way anyone can know your goal is to read your code.

    I'm sure you'll agree that reading your code may not be a very reliable way of understanding your goal, given that right now your code isn't doing what you want.

    Best, beth

      Sorry for beeing late.I need an output like this:
      0 440Z0220-932§other information <-- this is the first line of the + "root file" 1 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 7010900H01other 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 1 623Z5420-3301§other information 1 623Z5465-41§other information 1 623Z5462-1001§other information <-- this is the line that has chil +dren of level 2 2 623Z5465-1§other information <-- this is the second line of the + 1_623Z5465-1001.CSV file 2 623Z5465-11§other information 2 CA623Z5465-1001§other information <-- and this is the last line of t +he 1_623Z5465-1001.CSV file 1 623Z5462-1003§other information <-- here i continue to read the "r +oot" file 2 623Z5468-1§Other information <-- this is the second line of the + 1_623Z5462-1003.CSV 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 <-- and this is the last line of t +he 1_623Z5462-1003.CSV 1 7010902H01§other information <-- continue to read the "root" fi +le 1 623Z5420-3307§other information 1 B3-15838§other information 1 623Z5422-3307§other information

        Note: OP has updated the post above with a much more detailed annotated explanation, so this answer may not make sense anymore.

        Sorry for my slowness, but I didn't quite follow originally what you meant. I think I get it now. The first part of the line ("440Z0220-932") identifies a file. The duplicate lines associated with "440Z0220-932" are for each line within the file identified by "440Z0220-932". Is that right? So if you had only the following inputs:

        # FILE #1: 0_IP_017.CSV (reduced to illustrate logic) 623Z5465-1001§other information 623Z5462-1003§other information # FILE #2: 1_623Z5465-1001.CSV 623Z5465-1001§other information 623Z5465-1§other information 623Z5465-11§other information CA623Z5465-1001§other information # FILE #3: 1_623Z5462-1003.CSV 623Z5462-1003§Other information 623Z5468-1§Other information 623Z5468-9§Other information 623Z5468-7§Other information 623Z5468-5§Other information CA623Z5462-1003§Other information BACD40AC36B30§Other information 623Z5469-7§Other information 623Z5462-3§Other information

        You would get the following output file?

        1 623Z5465-1001§other information #line in file #1 - ids file #2 2 623Z5465-1001§other information #line in file #2 2 623Z5465-1§other information #line in file #2 2 623Z5465-11§other information #line in file #2 2 CA623Z5465-1001§other information #line in file #2 1 623Z5462-1003§other information #line in file #1 - ids file #3 2 623Z5462-1003§Other information #line in file #3 2 623Z5468-1§Other information #line in file #3 2 623Z5468-9§Other information #line in file #3 2 623Z5468-7§Other information #line in file #3 2 623Z5468-5§Other information #line in file #3 2 CA623Z5462-1003§Other information #line in file #3 2 BACD40AC36B30§Other information #line in file #3 2 623Z5469-7§Other information #line in file #3 2 623Z5462-3§Other information

        Best, beth

        Update: finally got what OP meant and updated accordingly. The striken portion is below.

        This is much better, but it still isn't clear why, for example, the output has two lines with "440Z0220-932":

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

        but only one of your sample input files (0_IP_017.CSV) has a line with "440Z0220-932" in it.

        Nor is it clear how you are defining that something is a node and what node number (0,1,2) to attach to it. For example, why is "440Z0220-932" assigned both 0 and 1 when it is only in the one non-"leaf" file? This doesn't seem consistent with your explanation above (at least to me).

        Perhaps you could give us a very small set of short input files and an output file with just a few sample lines and then explain (in words) the reasons why you assigned 0,1, or 2 to each of the output lines.

Re: create a level csv
by Utilitarian (Vicar) on Apr 16, 2009 at 08:44 UTC
    Hi superdoc, have you considered checking the return status of your file opens?
Re: create a level csv
by ig (Vicar) on Apr 16, 2009 at 11:10 UTC

    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.

      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.