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

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.