Brothers, I have written an application to:
1. Take a directory full of files
2. Create a temp file with 2 lines to prepend
3. Read the original file and extract first and last line timestamps
4. Copy the timestamps to the tmp file (2nd prepended line)
5. Append the lines from the original to the tmp file
And finally
6. Rename the tmp to the original filename.
The code is below
My question is: There is a great deal of redundancy in the first subroutine. How can I compact this? It seems like in order to get the first and last line timestamps, I have to do a series of opening and closing file handles. There must be a better way to accomplish this. I am looking for a way to make it cleaner/neater. The actually works, but its not pretty. I will clean this up by using strict & warnings (I forgot to use those..but hey, like I said, it does the job). Thanks

#! perl.exe -w # Perl Script to change format of Unigen .csv files from version 17 to + version 24. # This script will add two lines to the beginning of each file to acco +modate ARAMS Access Database # and copy the file to a final directory destination. use File::Copy; $indir = "c:/public/conversion"; $outdir = "c:/public/final"; opendir DH,$indir or die "Cannot open $indir: $!"; ## has all the files and directories in the given ## directory. You'll want to screen it to make sure you're ## opening a file. foreach $file(readdir DH) { $name = $file; next if $name =~ /^\./; # skip over dot files &addheader($_); } close DH; sub addheader { open FH, "< $indir/$file" or printf "can't open %s\n",$file; $line = <FH>; $_ = $line; close FH; $field1 = m/^Clock/; # Check for C +lock at beginning of file printf "\n$file"; if ( $field1 == "1"){ printf "\tv17\tFile Needs Appending\n"; open FH1, "< $indir/$file" or printf "can't open %s\n", $file; + # Open source file 1 while <FH1>; my $count = $.; # Gets the tota +l number of lines in file close FH1; open FH1, "< $indir/$file" or printf "can't open %s\n", $file; + my $line=<FH1>; $.= 1; my $number = 2; # Gets beginnin +g of file timestamp - fourth line from the beginning of the file print "$file\n"; do {$line =<FH1>} until $. == ($number); my @fields = parse_csv($line); print "$fields[0]\n"; my $start = $fields[0]; do {$line =<FH1>} until $. == ($count); # Get +end of file timestamp - last line @fields1 = parse_csv($line); my $end = $fields1[0]; close FH1; open FH1, "< $indir/$file" or printf "can't open %s\n", $file; + open FH2, '> c:/public/final/tmp.csv' or die "can't append: $! +" ; # Open target file - create if not there already print FH2 "PREPEND LINE1\n"; # Prepend + text print FH2 "blank,"."blank,"."blank,"."$start,"."$end\n"; + # Prepend first & last timestamp my @lines = <FH1>; foreach $line ( @lines ) # Append tmp f +ile with original files { print FH2 $line; } close FH1; close FH2; $oldfile = 'c:/public/final/tmp.csv'; $newfile = "$outdir/$file"; rename $oldfile,$newfile or die"can't rename files\n"; + # Rename tmp file to original filename print "$oldfile\n"; } else { printf "\tv24\tFile Does Not Need Appending\n"; copy "$indir/$file", "$outdir/$file"; } return ; } sub parse_csv { my $text = shift; my @new = (); push(@new, $+) while $text =~ m{"([^\"\\]*(?:\\.[^\"\\]*)*)",? | ( +[^,]+),?| ,}gx; push(@new, undef) if substr ($text, -1,1) eq ','; return @new; #list of values that were comma-seperated }

janitored by ybiC: balanced <readmore> tages around code


In reply to Style Comments by mgolini

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.