skyler has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I really need your help. I have been working with Perl for the past few month but I'm not an expert. I'm seeking your expersite on this task. Basically, I open and read a text file, then I start to read it line by line. Then I replace commas by pipes and grab the path to format it; then I pass the newly created path to a loop to start finding RTF files that belong ro each directory that I'm searching on. Then copy and rename the file in a specify format. I also create a text file with each line from the parse file. I'll appreciate your help.
BEFORE 484257-1,DE ARMAS,BLANC,Cristiane Takita,1/21/03,MD Wkly Note,H:\APPS\ +IMPAC\DB\ESCRIBE\00\00004EAC.006 467488,FOURNIER,JANET,Cristiane Takita,1/22/03,MD Wkly Note,H:\APPS\IM +PAC\DB\ESCRIBE\12\00004E04.012 306684,SECHI,OSVAL,Aaron Wolfson,1/22/03,MD Wkly Note,H:\APPS\IMPAC\DB +\ESCRIBE\08\00004E14.022 310941,PAUL,TAMEA,B-Chen Wen,1/22/03,MD Wkly Note,H:\APPS\IMPAC\DB\ESC +RIBE\01\00004E35.009
AFTER
484257|DE ARMAS|BLANC|Cristiane Takita|1/21/03|MD Wkly Note|H:\00\0000 +4EAC.006 467488|FOURNIER|JANET|Cristiane Takita|1/22/03|MD Wkly Note|H:\12\0000 +4E04.012 306684|SECHI|OSVAL|Aaron Wolfson|1/22/03|MD Wkly Note|H:\08\00004E14.0 +22 310941|PAUL|TAMEA|B-Chen Wen|1/22/03|MD Wkly Note|H:\01\00004E35.009
Desired output Newly created text file / each per line parse
310941|PAUL|TAMEA|B-Chen Wen|1/22/03|MD Wkly Note@@H:\01\00004E35.009\ +31094100004E35009.rtf
Newly created file 31094100004E35009.rtf
#! perl -w ########################################## # Program to automate document extraction ########################################## use strict; #################### # Read Template File #################### sub main { my $infile = 'c:/doclist.chr'; my $outfile = 'c:/doclist.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; ## print OUT join '|', split /,/ while <IN>; while(<IN>) { chomp; my @fields = split /,/; my $path_str = $fields[6]; do { warn "Empty field 7"; next } unless $path_str; my @path = split /\\/, $path_str; # assuming you want to remove a few directories my $fixed_path = join "\\", @path[0,5,6]; &find_copy_rename; &create_text_file; } exit; close IN; } ########################### # End of Read Template File ########################### ############################################# # Find, Copy, and Rename File to MRN+Date.rtf ############################################# use File::Find; use File::Copy; sub find_copy_rename { my @dir = $fixed_path; my $dir = @dir; if ($File::Find::dir ne $dir) { $File::Find::prune = 1; return 0; } return 0 if ($_ !~ /\.rtf$/); copy($File::Find::name, "C:\\temp\\$_") or die "Failed to copy $_: + $!\n"; return 1; } find(\&process_files, $dir); my @change_files = grep { !-d } @all_files; foreach my $get_files (@change_files) { my $newfile = $get_files; $newfile =~ s/\$mrn.$now.rtf$/word1.rtf/; if (-e $newfile) { warn "can't rename $get_files to $newfile: $newfile exists\n"; } elsif (rename "$newdir/$get_files", "$newdir/$newfile") { print "file was renamed to $newfile\n" } else { warn "rename $get_files to $newfile failed: $!\n"; } } ############################### # Create Text File per each MRN ############################### sub create_text_fle { foreach my $text_file (@fields[0]) { if ($mrn = @fields[0]) { my $newpatientrec = $newfile; open NEWPATIENT,">$newfile" or die "Couldn't open $outfile, $ +!"; close NEWPATIENT; } my $out = join '|', @fields[0..5], $fixed_path, "\n"; print OUT $out; } }

Replies are listed 'Best First'.
Re: Open File and Parse file line by line
by Marza (Vicar) on Feb 24, 2003 at 02:48 UTC

    Map is your friend!!!!!! jeffa wrote a nice little tutorial about it here

    A quick example that "should" help on your comma to pipe switch issue.

    open (FILE, "foo.txt"); my @lines = map { s/,/|/g; $_; } <FILE>; close FILE;

    Mind you this is just a snippet taken and modified from the tutorial. You might need a little more "tunning" for your needs.

Re: Open File and Parse file line by line
by dvergin (Monsignor) on Feb 24, 2003 at 02:51 UTC
    Your indenting and placement of curly brackets is somewhat inconsistent and, in places, misleading, but...

    As best I can see at quick glance, your code consists of three subroutines. sub main calls find_copy_rename and create_text_file. But sub main itself is never called.

    Perhaps you thought "main" would fire automatically. That is not the case in Perl.

    Also, I would encourage you to call your subroutines with sub_name() rather than &sub_name. The latter form exposes @_ from the calling context in a way that could cause you confusion inside your sub.

    Hope that helps.

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

Re: Open File and Parse file line by line
by jasonk (Parson) on Feb 24, 2003 at 01:52 UTC

    You would get more answers, if your post included a question.

      i need helh to get my desired output how do i achieve it?