in reply to Re: help merging perl code routines together for file processing
in thread help merging perl code routines together for file processing
Hi, thanks again for your help. I been doing some testing with this and I'm stuck. The match succeeds and it gets into the subroutine as I put a print statement. But there I'm trying to figure out your use of the following line of code:
my ($filename) = @_;Can you elaborate more on that part and what you meant by "Need to get arguments here?"
Here's an update with some additional checks. Thank you.
use strict; use Digest::MD5 qw(md5_hex); # from first script my $target_dir = "/backups/test/"; my $regex_flag = 0; 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"; $regex_flag=1; #Set to true } if ($regex_flag==0) { print "No matching files found \n"; exit; } print "$file\n"; print "We have a match.\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: print "Starting process file..\n"; my ($filename) = @_; # from second script #Create new file open NEWFILE, ">", "/backups/processed/foo$1.name.$2-foo_p$3.out" or 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 = "__pkey__$sep1$digest$sep2" ; $extra .= "$heading[$_]$sep1$values[$_]$sep2" for (0..scalar(@ +values)); $data .= "$extra$eorec"; print NEWFILE "$data"; } #print $data; close (NEWFILE); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: help merging perl code routines together for file processing
by roboticus (Chancellor) on Feb 20, 2011 at 07:30 UTC | |
by JaeDre619 (Acolyte) on Feb 21, 2011 at 00:25 UTC |