RE: New to Perl and stuck
Just in the simplest and most general of terms, what it sounds like you want to do is an opendir on the folder, then use readdir to get each of the file names. Store each filename in a variable, and use it in the open command to open the file. You might want to put the code that actually extracts the lines you want into a separate subroutine, and just call that subroutine for each file you open (or for each filename if you open the file in the subroutine). After you process your file, then use the close to close the file so you can move on to the next one and process it. It sounded from your description like you were looking for what opendir does. To get an explanation, do this: perldoc -f opendir See if that gets you started. Ask back if you need more specifics. Steve Howard -Original Message- From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Bryan Keller Sent: Tuesday, March 03, 2009 9:45 AM To: perl-win32-users@listserv.ActiveState.com Subject: New to Perl and stuck Hi all, The following gets me the lines I need for any single txt file. #!usr/bin/perl use warnings; use strict; my @output = <>; print "$ARGV"; print ("\nESTIMATION\n"); print @output[18,21..24,28..31,206..208]; print "\nPOPULATION\n"; print @output[220,223..226,229..233,424..426]; These txt files are in groups of 25 (in folders). My goal is to automate this so that after specifying a folder, Perl will pull the specified lines from each of the 25 txt files in that folder and write them to a single output file. I would greatly appreciate any help or suggestions! Bryan - Bryan Keller, Doctoral Student/Project Assistant Educational Psychology - Quantitative Methods The University of Wisconsin - Madison ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: New to Perl and stuck
Hi. Try this with the directory name as the command line argument. Let me know if u have any questions. I really recommend getting one of the O'Reilly books to really get into Perl. %estparms = map {$_, 1} (18,21..24,28..31,206..208); %popparms = map {$_, 1} (220,223..226,229..233,424..426); $dir = $ARGV[0] or die; @files = glob "$dir/*.txt"; foreach $file (@files) { my $lineno = 0; open FILE, $file; while (my $line = ) { push @estlines, $line if exists $estparms{$lineno}; push @poplines, $line if exists $popparms{$lineno}; $lineno++; } close FILE; print "FILE\n$file\n"; print "ESTIMATION\n", join "\n", @estlines, ""; print "POPULATION\n", join "\n", @poplines, ""; } At 11:44 AM 3/3/2009 -0600, Bryan Keller wrote: >Hi all, > >The following gets me the lines I need for any single txt file. > >#!usr/bin/perl >use warnings; >use strict; > >my @output = <>; >print "$ARGV"; >print ("\nESTIMATION\n"); >print @output[18,21..24,28..31,206..208]; >print "\nPOPULATION\n"; >print @output[220,223..226,229..233,424..426]; > >These txt files are in groups of 25 (in folders). My goal is to automate this so that after specifying a folder, Perl will pull the specified lines from each of the 25 txt files in that folder and write them to a single output file. > >I would greatly appreciate any help or suggestions! > >Bryan > >- >Bryan Keller, Doctoral Student/Project Assistant >Educational Psychology - Quantitative Methods >The University of Wisconsin - Madison >___ >Perl-Win32-Users mailing list >Perl-Win32-Users@listserv.ActiveState.com >To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede malis" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: New to Perl and stuck
> My goal is to automate this so that after specifying a folder, Perl will pull the specified lines from each of the 25 txt files in that folder and write them to a single output file. If you don't need file names etc, you can just expand your pointies to use a glob @output = <*.txt> but that put it all in there, so: print ("\nESTIMATION\n"); print @output[18,21..24,28..31,206..208]; print "\nPOPULATION\n"; print @output[220,223..226,229..233,424..426]; would need to be done in ... increments of 427 or whatever the total lines/file is. That seem fraught w/ peril but you know your data best. I'd never trust line #s to be accurate, nor would I believe in fixed length data files, but only as I've been well burned by both assumptions. YMMV You can use command line globbing (if you've got it): myapp /this/data/*.txt and then each file is opened one after the other. perldoc -f eof will help if want to handle them one after the other. Otherwise, opendir and readdir are the route - perldoc -f readdir gives the better sample code. a --- Andy Bach Systems Mangler Internet: andy_b...@wiwb.uscourts.gov Voice: (608) 261-5738; Cell: (608) 658-1890 Entropy just ain't what it used to be___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs