No you may not pull my code apart ;) Thanks for all good advice. However why is the & in front of the sub routine call a bad idea. From what I understood from the learning perl (it has been awhile since I looked at this book) you should always put the & in front of the call unless your subroutines are defined at the top of your code. Maybe I misunderstood something.
Paul -----Original Message----- From: Rob Dixon [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2003 12:52 PM To: [EMAIL PROTECTED] Subject: Re: Looping with multiples? Hi Paul. May I pull your code apart? Paul Kraus wrote: > I have a variable amount of text files that need to get parsed. The > files come in groups of 6. I only need to parse files 4 5 and 6. So if > I had 3 groups waiting to be processed then I would have to read > through and parse 4,5,6,10,11,12,16,17,18. > > The file names look like this... > > 6.1.200_(1)_(9443).txt > Date_element of group_time.txt > > So I would processing all of the files for 6.1. > Also I couldn't figure out a way to sort them easily as you will see > me in my code so any suggestions there would be helpful also. > > How can I setup a loop that will run the correct amount of times and > only parse these files from the group. Here is my code. > > > #!/usr/bin/perl > > use warnings; > use strict; > use DBI; > > opendir ( DH, "." ) or die ( "Could not open $!\n" ); > my @allfiles = readdir(DH); You don't need @allfiles. You can do foreach ( readdir DH ) { : } > my %sales; > foreach ( @allfiles ) { > next unless /^\d+\.\d+\.\d+_/; > push (@{$sales{$1}}, $_) if /^(\d+\.\d+\.\d+)_/; > } No need to check the filename twice - just my %sales; foreach ( readdir DH ) { push (@{$sales{$1}}, $_) if /^(\d+\.\d+\.\d+)_/; } > foreach (sort keys %sales){ > my $salesjournals = scalar(@{$sales{$_}}); > &processdate($sales{$_}, $salesjournals); > die; > } No need to calculate the array size before the call, and the & on the subroutine name is a bad idea. foreach (sort keys %sales){ processdate($sales{$_}); } This then needs sub processdate { my $filenames = shift; my $salesjournals = @$filenames; > sub processdate { > my ($filenames, $salesjournals) = ( shift, shift ); > my (%pel, %cfb, %leimkuehler, @files); > foreach ( @ { $filenames } ){ > push (@files, $1 . "-$_") if /_\((\d+)\)_/; > } Use 'grep' here to pull out the files you want. I assume they have names _(1)_ upwards? my @files = grep { /_\((\d+)\)_/, ($1 - 1) % 6 > 2 } @$filenames; > @files = sort {$a <=> $b} @files; > foreach (@files){ > my $tmp = $1 if s/^(\d+)-//; This can never be true. Your filenames look like /^\d+\./. Will /^(\d+)/; print "$1 \t $_\n"; do here? > if ( $tmp == ){ > print "$tmp \t $_\n"; > } > } > } Anyway, here's the assembled package. I hope it helps. Rob #!/usr/bin/perl use warnings; use strict; use DBI; opendir DH, '.' or die "Could not open directory: $!"; my %sales; foreach ( readdir DH ) { push @{$sales{$1}}, $_ if /^(\d+\.\d+\.\d+)_/; } foreach (sort keys %sales) { processdate($sales{$_}); } sub processdate { my $filenames = shift; my $salesjournals = @$filenames; my @files = grep { /_\((\d+)\)_/, ($1 - 1) % 6 > 2 } @$filenames; @files = sort {$a <=> $b} @files; foreach (@files){ my ($tmp) = /^(\d+)/; print "$tmp \t $_\n"; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]