Dear Monks,
I need some guidance in putting these (2) processes/code to work together. Most of my experience with perl is doing basic file processing, just a few lines of code.
Anyways, I was testing these (2) pieces of code and I am stuck figuring out how to get them to work together.
I was able to get them working individually to test, but I need help bringing them together especially with using the loop constructs and producing a new output file after reading and processing the input file. I'm not sure if I should go with a foreach, while..anyways the code is below.
Also, any best practices would be great too as I'm learning this language. I was told I was writing this in an old style perl programming. I'm sure I am. Any insight and getting these pieces to work smoothly would be appreciated. Thanks for your help.
Here's the process flow I am looking for: -read a directory -look for a particular file -use the file name to strip out some key information to create a newly processed file -process the input file -create the newly processed file for each input file read (if i read in 10, I create 10 new files)
Part 1:
my $target_dir = "/backups/test/"; opendir my $dh, $target_dir or die "can't opendir $target_dir: $!"; while (defined(my $file = readdir($dh))) { next if ($file =~ /^\.+$/); #Get filename attributes if ($file =~ /^foo(\d{3})\.name\.(\w{3})-foo_p(\d{1,4})\.\d+.csv$/ +) { print "$1\n"; print "$2\n"; print "$3\n"; } print "$file\n"; }
Part 2:
use strict; use Digest::MD5 qw(md5_hex); #Create new file open (NEWFILE, ">/backups/processed/foo$1.name.$2-foo_p$3.out") || die + "cannot create file"; my $data = ''; my $line1 = <>; chomp $line1; my @heading = split /,/, $line1; my ($sep1, $sep2, $eorec) = ( "^A", "^E", "^D"); while (<>) { my $digest = md5_hex($data); chomp; my (@values) = split /,/; my $extra = "__mykey__$sep1$digest$sep2" ; $extra .= "$heading[$_]$sep1$values[$_]$sep2" for (0..scalar(@valu +es)); $data .= "$extra$eorec"; print NEWFILE "$data"; } #print $data; close (NEWFILE);
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |