Hi,
I was hoping you might help me figure this part out. I'm able to now print to a file. I think I had to open the file handle for the file I found and used that to process and then created a file handle for the new processed file. It works to an extent, but I am creating some duplicate records. I put a print statement on $data and to view on terminal output and the number of recs look ok, but not so on the outputfile. What am I doing wrong? Thanks again!
My sample records:
col1,col2,col3,col4,col5 coor@tra.co.nz#hv,mac-l@lists.listmoms.net,8,2009-09-24 21:00:46,1 vw@tra.co.nz#i3,poalad888@test.com,16,2007-08-18 22:53:12,33 esmith@tra.co.nz#hv,gabmonsh@mymail.com,16,2007-08-18 23:41:23,33
Updated code
use strict; use Digest::MD5 qw(md5_hex); 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 =~ /^\.+$/; if ($file =~ /^foo(\d{3})\.name\.(\w{3})-foo_p(\d{1,4})\.\d+.csv$/ +) { process_file($file, $1, $2, $3); } } sub process_file { my ($filename, $foo_x, $name_x, $p_x) = @_; my $new_name = "/backups/processed/foo$foo_x.name.$name_x-foo_p$p_ +x.out"; open my $in_fh, '<', $filename or die "cannot read $filename: $!"; open(my $out_fh, '>', $new_name) or die "cannot create $new_name: +$!"; my $data = ''; my $line1 = <$in_fh>; chomp $line1; my @heading = split /,/, $line1; my ($sep1, $sep2, $eorec) = ("^A", "^E", "^D"); while (<$in_fh>) { chomp; my $digest = md5_hex($data); my (@values) = split /,/; my $extra = "__mykey__$sep1$digest$sep2"; $extra .= "$heading[$_]$sep1$values[$_]$sep2" for (0 .. scalar(@values)); $data .= "$extra$eorec"; print $out_fh $data; print $data; } close $out_fh or die "Failed to close file: $!"; }
In reply to Re^4: help merging perl code routines together for file processing
by JaeDre619
in thread help merging perl code routines together for file processing
by JaeDre619
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |