Re: regex help please



"J" == Jerry <jpreston@xxxxxxxxxxxxxxxxx> writes:

J> #!/perl

J> use strict;

use warnings ;

J> local( *FI, *FO );

why are you using localized file handles? use lexical handles or use
File::Slurp which will be simpler and cleaner.

J> my $DEBUG = 10;
J> my $DEBUG1 = 0;

J> my $work_path = "C:\\Documents and Settings\\Jerry\\My Documents";
J> my @Form_files = ();
J> my %Form_data = ();

no need to assign () to my hashes or arrays. they are always empty. also
declare vars where they are first used.

J> my $screen_cnt = 0;

J> ## read dir where files exist and get only the file we are interested in
J> opendir( DIR, $work_path );

why the localized file handles and not a dirhandle?

J> @Form_files = grep( /^PAS/,/DFM/, readdir( DIR ));

File::Slurp also has a read_dir function for that. no need for an
opendir call and dirhandle then.

that grep makes no sense. the second arg /DFM/ is not operating on $_ in
the grep. my guess is that it is operating on $_ which is not even set
so it would give a warning. hence use warnings would have found
this. what is your intent here, to get lines with either PAS or DFM?

J> ## sort filies
J> @Form_files = sort { $a cmp $b } @Form_files;

text sorting is the default for sort so you can drop the compare block

with File::Slurp that could all become this line:

my @form_lines = grep /^PAS/ || /DFM/, read_dir( $work_path ) ;

that eliminated about 3 lines of code


J> print "\n\n@Form_files\n\n" if $DEBUG;

J> ## start of breaking down of form files
J> foreach my $form_file ( @Form_files ) {

J> open ( FI, "$work_path\\$form_file" ) or die "Couldn't open
J> $work_path\\$form_file";
J> my @text = <FI>;
J> close FI;

those three lines and the local() call can be replaced with:

my @text = read_file( "$work_path/$form_file" ) ;

notice that i used / as that works on winblows as well as unix flavors.

J> $form_file =~ s/DFM/FDT/; # swap out file ext DFM to FDT -> Form Data

J> # print "opf - $form_file\n";

J> ## find GENERALto find screen title
J> my $cnt = -1;
J> foreach ( @text ) {

i prefer to use named variables over $_ whenever possible.


J> ## break out be screen #, exp: '(Screen 2)'
J> print $screen_cnt++ if $_ =~ /\(Screen /i;

but if you do use $_ there is no need to use it explicitly.

print $screen_cnt++ if /\(Screen /i;

J> print "$screen_cnt screen $_!!" if $_ =~ /\(Screen /i;

J> $cnt++;
J> print "cnt $cnt - $form_file - $_";
J> next if $_ !~ /GENERAL/i;
J> last;

why the next/last there? you could invert the boolean (i never use !~
anyhow) with unless and just call last then. next will fallout since
this is the end of the loop.

last if /GENERAL/i;

J> }
J> # open ( FO, ">+$work_path\\$form_file" ) or die "Couldn't create
J> $work_path\\$form_file";
J> # print FO "$_";

write_file( $work_path/$form_file", $_ ) ;


J> print "parm: *1 $1*2 $2*3 $3*4 $4*6 $6*7 $7*8
J> $8*$9*$10*$11*$12*$_*<br>\n";


dunno where all those $1 vars were set. and that is a strange reading
form of output.

and stop top posting. read the group guidelines which are posted
regularly.

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.