#!/usr/bin/perl ####################################### ## test.pl ## Program to parse a txt file given in ## tab-delimited format of physio data ## Parses all files in a directory and ## compiles the results into one file ## Looks through desired folders in dir ####################################### $dir_path = 'G:\\Test Data'; @dir_folds = ('Ts1', 'Ts10', 'Ts12', 'Ts13', 'Ts14', 'Ts15', 'Ts16', 'Ts17', 'Ts18', 'Ts19', 'Ts2', 'Ts20', 'Ts21', 'Ts22', 'Ts23', 'Ts24', 'Ts25', 'Ts26', 'Ts27', 'Ts3', 'Ts4', 'Ts5', 'Ts6', 'Ts7', 'Ts8', 'Ts9'); ## holds the folders at $dir_path where desired files are located $full_path; ## Full directory path $avg_file = ">>averages.txt"; ## Name of file averages are written to (oppened for appending) $full_name; ## Full file name (i.e., with directory path) $ln_num = 0; ## Line number, used in @zeroat array $i = 0; ## Reference counter $j = 0; ## Reference counter (specifically for @avg_frmt array; counts through file loop) $sum1 = 0; ## HRT sum $sum2 = 0; ## SKT sum $sum3 = 0; ## EMG sum $avg11 = 0; ## HRT avg 1 $avg12 = 0; ## HRT avg 2 $avg21 = 0; ## SKT avg 1 $avg22 = 0; ## SKT avg 2 $avg31 = 0; ## EMG avg 1 $avg32 = 0; ## EMG avg 2 @files; ## Array to hold desired filenames @avg_frmt; ## Array to hold formatting for $avg_file document @lines; ## Array to hold lines of file @lines1; ## Holds first part to be aberaged @lines2; ## Holds second part to be averaged @zeroat; ## Tells where zeros are at in array @lines ## Do procedure for all desired folders at $dir_path for $dir_fold(@dir_folds) { ## Print Status: i.e., which folder the program is currently on print "$dir_fold\n"; ## Retrieve all .txt files at $dir_path\$dir_fold $full_path = "$dir_path\\$dir_fold"; opendir(DIR, $full_path) or die "$full_path failed to open: $!"; @files = grep { /\.txt$/ } readdir(DIR); closedir(DIR); ## Begin looping over files, compiling averages for each for $i_file(@files) { next if $i_file =~ /RAREEVENT/; $full_name = "$full_path\\$i_file"; open(IN,$full_name) or die "$i_file failed to open: $!"; @lines = ; ## Print Status: i.e., which file the program is currently on print "\t$i_file\n"; ## Retrieve desired rows for $curline(@lines) { $curline =~ /.*?\t.*?\t.*?\t.*?\t([05])/; ## parse line $zeros[$ln_num] = $curline; $zeroat[$i] = $ln_num if $1 == 0; $ln_num++; $i++; } ## Take Average ## Get all points between the starting and ending points, and separate into different arrays for $i(@zeroat) { ## $i is an index in @lines to where a zero is at $lines[$i] =~ /(.*?)\t.*?\t.*?\t.*?\t([05])/; ## parse line if ($1 > .5 && $2 == 0) { @lines1 = @lines[0..$i-1]; ## @lines1 equals the first $i-1 elements of @lines @lines2 = @lines[$i+1..$#lines]; ## @lines2 equals everything past the $i+1 element of @lines @lines = (@lines1,@lines2); ## @lines equals @lines1 followed by @lines2 ({$i}th element removed) } ## the zero is in the middle: split for averaging } ## Reset sums $sum1 = 0; $sum2 = 0; $sum3 = 0; for $i(@lines1) { ## go through first part and average $i =~ /.*?\t(.*?)\t(.*?)\t(.*?)\t5/; ## parse line $sum1 += $1; $sum2 += $2; $sum3 += $3; } ## Get first average $avg11 = $sum1/$#lines1; $avg21 = $sum2/$#lines1; $avg31 = $sum3/$#lines1; ## Reset sums $sum1 = 0; $sum2 = 0; $sum3 = 0; for $i(@lines2) { ## go through second part and average $i =~ /.*?\t(.*?)\t(.*?)\t(.*?)\t5/; ## parse line $sum1 += $1; $sum2 += $2; $sum3 += $3; } ## Get second average $avg12 = $sum1/$#lines2; $avg22 = $sum2/$#lines2; $avg32 = $sum3/$#lines2; ## Put averages into tab delimited columns with desired format: File name followed by tab followed ## by averages; first line is resting condition; second line is cloud condition. $avg_frmt[$j] = "$i_file\t$avg11\t$avg21\t$avg31\n"; ## HRT, SKT, EMG is the $avg_frmt[$j+1] = "$i_file\t$avg12\t$avg22\t$avg32\n"; ## order for the averages $j += 2; } ## End looping over files in folder } ## End looping over folders in directory ## Open and print averages to $avg_file open(OUT,$avg_file) or die "$avg_file failed to be created: $!"; print OUT @avg_frmt;