JaeDre619:

Well both scripts are simple enough to include the second one inside the while loop of the first one, something like this:

# from second file use strict; use Digest::MD5 qw(md5_hex); # from first file 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"; # From second file #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(@ +values)); $data .= "$extra$eorec"; print NEWFILE "$data"; } #print $data; close (NEWFILE); # and back to the first file }

Note: I didn't change anything except moved a couple of blocks of code (marked) and changed the indentation (should be clear enough). You'll have to make any fixes.

Rather than that, though, I think I'd just make the second script a subroutine, and call it from the first script. Again, something like the following. (Again, I'm not changing anything, just arranging stuff.):

# from second script use strict; use Digest::MD5 qw(md5_hex); # from first script 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"; # New line added: call the routine to process file process_file($file); } # New line: tell perl that we're making a subroutine sub process_file { # Need to get arguments here: my ($filename) = @_; # from second script #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(@ +values)); $data .= "$extra$eorec"; print NEWFILE "$data"; } #print $data; close (NEWFILE); }

Oh, yes, you asked for some best practice suggestions, too. The only ones that jump out at me are:

Update: Added readmore tags and best practice suggestions.

Is this the kind of suggestion you were seeking?

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: help merging perl code routines together for file processing by roboticus
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.