http://qs321.pair.com?node_id=722563

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

Hi Monks,

I need ur help in solving this stuff. My req is ...

1. I have files SU1, SU2 ...like that . But the total number of files is unknown...there might be 7 or 6 files....

2. Similarly i have MU files -> MU1, MU2 etc...

3. These are tab delimited content. Each tab has Numbers..

Sample format is:
TextString1 \t 23.67 \t 57 \t 90.8 TextString2 \t 20.9 \t 58 \t 99
4. Now I have to collect only the 2nd column from each file and put under output file
5. Output header is:
Text String \t SU1 \t SU2....\t SU-Average \t MU1 \t MU2......\t MU-Av +erage TextString1 \t [value from Row 1 - Column 2 of SU1] \t [value from Row + 1 - Column 2 of SU2] \t [Avg of SU1 & SU2] ...like that
My approach is:
1. Read the contents and parse the 2column ..save it in Array..named @SU1 etc..
2. Then in a for loop ...print the rows in to a file
for(my $i = 0; $i < $total_lines_in_each_file; $i++){ print(OUTPUT "$TextString[$i]\t$su_first[$i]\t$su_second[$i]\t$su_thir +d[$i]\t$su_average\t$mu_first[$i]\t$mu_second[$i]\t$mu_third[$i]\t$mu +_average \n"); }
The output will be
Text String \t 57 \t 58 \t Avg(57,58)
My Problem:

1. Here the arrays SU_first etc are hardcoded. Ie I have restricted to only 3 arrays. If there are SU4 etc I cant use this script How to give a Dynamic name for array and retrieve it..?

2. Or any other better approach?!!!

Finally from n number of tab limited files take only the 2nd column, find the average of each row and print it in another file.

Pls help me. Thanks in advance!

(Original content above, restored by Arunbear. Updated content follows:)

Hi Thanks for the response..But I'm not able tomend it as per my req..(I need some more expertise in PERL) here is what I need..
su1_file 1 "abc" 6 8 "def" 7 9 "ghi" 2 5 su2_file 2 "abc" 60 8 "def" 70 900 "ghi" 20 5 su_file 3 "abc" 600 8 "def" 700 900 "ghi" 200 5 Output file: Header su_file1 su_file2 su_file3 average ...mu1_ mu2_ mu3_ etc.... abc 6 60 600 *** def 7 70 700 *** ghi 2 20 200 ***
so @su1 will have 6,7,2 @su2 will have 60,70,20 etc..But I dont know how many such files in mu and su..there might be Su9, su10 etc. Runtime I need to check the number of files, get those values and print to one single file. Once I parse the values from each file, i have to print the values in columnwise. I think the solution given by you does an average (some calculation) of all su values in column 1 .. Can u pls help me. ?? Now I have hard coded the number of files to only 3 in my code.

Replies are listed 'Best First'.
Re: Take Data out from unknown number of files
by jwkrahn (Abbot) on Nov 10, 2008 at 07:13 UTC

    If I understand you correctly, perhaps something like this is what you want:

    use List::Util 'sum'; my @su_data = do { local @ARGV = glob "SU*"; map( ( split /\t/ )[ 1 ], <> ); }; my @mu_data = do { local @ARGV = glob "MU*"; map( ( split /\t/ )[ 1 ], <> ); }; print join( "\t", 'Text String', @su_data, sum( @su_data ) / @su_data, + @mu_data, sum( @mu_data ) / @mu_data ), "\n";
    A reply falls below the community's threshold of quality. You may see it by logging in.