#!/usr/bin/perl ####################################### ## final_v02_TEST.pl ## test.pl (for short) ## 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 ## ## Possible Additions: ## ADDITIONS MADE ## ## final_v02_TEST.pl Version .02 (6/20) ## - ADDED 'close IN;' statement (6/21) ## - Code altered to only work on given ## text file (6/24) ## - NOTE USAGE: ## perl test.pl FILE_NAME DIR_PATH ####################################### $i_file; ## Name of input file $dir_path; ## Directory path for $i_file $full_name; ## '$dir_path\$i_file' $avg_file = ">>averages.txt"; ## Name of file where file averages are written to (append mode) $ln_num = 0; ## Line number in current file, used in @zeroat array to mark zeros $i = 0; ## Reference counter $j = 0; ## Reference counter (specifically for @avg_frmt array; counts through file loop) @sums[3]; ## Holds HRT, SKT, and EMG sums, respectively @avg->[3][2]; ## Holds 1st HRT, 2nd HRT, 1st SKT, 2nd SKT, 1st EMG, and 2nd EMG, averages, respectively @files; ## Array to hold desired filenames for current folder @avg_frmt; ## Array to hold formatting for $avg_file document (i.e., the formatted output) @lines; ## Array to hold lines of current file @lines1; ## Holds first part to be averaged @lines2; ## Holds second part to be averaged @zeroat; ## Tells where zeros are at in array @lines (holds the line number of the zeros; an index to @lines) $i_file = $ARGV[0]; ## Get file name from command line (first argument) $dir_path = $ARGV[1]; ## Get directory path from command line (second argument) $full_name = "$dir_path\\$i_file"; open(IN,$full_name) or die "$i_file failed to open: $!"; @lines = ; ## Give file input to @lines close IN; ## Retrieve desired rows for $curline(@lines) { ## $curline contains the current line being worked on (reverse $line) =~ /^\s*([05])/; ## Get 0 or 5 from end $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] =~ /^([0-9]+.?[0-9]*)\t.*([05])\s*$/; ## Get time (first column) and 0 or 5 (last column) if ($1 > .5 && $2 == 0) { ## {If} time ($1) is more than .5 {AND} end column ($2) is 0 ... splice @lines,$i,1; ## Remove $lines[$i] from @lines @lines1 = @lines; ## Copy neccessary for next statement @lines2 = splice @lines1,$i,$#lines1-$i+1; ## Splice removes desired elements from @lines1, ## which are given to @lines2 (splice's return value) } ## the zero is in the middle: split for averaging } ## Reset sums @sums = map { $sums[$_] = 0 } (0..2); for $i(@lines1) { ## go through first part and average @vals = split /\t/, $lines[$i]; ## Each column in the line has its own place in @vals map { $sums[$_] += $vals[$_] } (0..2); } ## Get first average map { $avg->[$_][1] = $sums[$_]/$#lines1 } (0..2); ## Reset sums @sums = map { $sums[$_] = 0 } (0..2); for $i(@lines2) { ## go through second part and average @vals = split /\t/, $lines[$i]; ## Each column in the line has its own place in @vals map { $sums[$_] += $vals[$_] } (0..2); } ## Get second average map { $avg->[$_][2] = $sums[$_]/$#lines1 } (0..2); ## 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$avg->[1][1]\t$avg->[2][1]\t$avg->[3][1]\n"; ## HRT, SKT, EMG is the $avg_frmt[$j+1] = "$i_file\t$avg->[1][2]\t$avg->[2][2]\t$avg->[3][2]\n"; ## order for the averages $j += 2; ## Open and print averages to $avg_file open(OUT,$avg_file) or die "$avg_file failed to be created: $!"; print OUT @avg_frmt;