grep
Hi, quick question...I am using grep to filter out files of certain extensions. for some reason, if i only want .c files, it leaves in .rc files. or if i were to specify .p, it would leave in .dsp and .cpp. However, if i were to specify .cpp, it would not leave in .c. any ideas? if so, please reply to me personally. Thanks, Tanya Graham ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: grep
$regex is right, until the second statement is executed. then it disappears. my $regex = $opt_e; my $regex = s/\s*,\s*/|/g; any ideas why? thanks tanya -Original Message- From: Arthur Cohen [mailto:[EMAIL PROTECTED]] Sent: Monday, June 04, 2001 12:02 PM To: Tanya Graham; [EMAIL PROTECTED] Subject: RE: grep : : : this is how it is called: : AddHeader.pl -f [directory name] -e [extension list] : this tells the program to add a header to the files with extensions : "extension list" in "directory name". I would actually : prefer to have them : comma separated, so can you explain how to replace the commas : with "|"? i : know i've seen that function before, but i can't remember what it is : called... Heh heh... it's called "s"... :) Assuming that your users might add some extra spaces around the commas: $extensions = '.c, .cpp , .h'; # set up some test data $extensions =~ s/\s*,\s*/|/g; # $extensions should now be '.c|.cpp|.h' ... you still need to escape the periods If you want to find out what the regular expression in the substitution command is doing, see the "perlre" reference. --Art ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: grep
this is how it is called: AddHeader.pl -f [directory name] -e [extension list] this tells the program to add a header to the files with extensions "extension list" in "directory name". I would actually prefer to have them comma separated, so can you explain how to replace the commas with "|"? i know i've seen that function before, but i can't remember what it is called... thanks tanya -Original Message- From: Arthur Cohen [mailto:[EMAIL PROTECTED]] Sent: Monday, June 04, 2001 11:38 AM To: [EMAIL PROTECTED] Subject: RE: grep : : : if i take the argument from a flag (so i won't be using : @ARGV, but opt_x) : would i still be able to use join? You can do what you want to do, but I'm not sure exactly what you're trying to do. How are you calling the program from the command line, and how are you pulling the command-line arguments into variables? If the file extentions are already in a single scalar variable (e.g. comma-separated or something) rather than an array like @ARGV, then you may need to do a split first, then a join, or you may just be able to separate your comma separator (or whatever) with a | character. --Art ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: grep
if i take the argument from a flag (so i won't be using @ARGV, but opt_x) would i still be able to use join? thanks tanya -Original Message- From: Arthur Cohen [mailto:[EMAIL PROTECTED]] Sent: Monday, June 04, 2001 11:28 AM To: Tanya Graham; [EMAIL PROTECTED] Subject: RE: grep You can assemble the regular expression as a string and then use that. So if your users call the script like: foo.pl .c .cpp .h .rc you could do something like this: my $regex = join ("|", @ARGV); # $regex is now '.c|.cpp|.h|.rc' $regex =~ s/\./\\./g; # escape the periods my @filtered = grep { /($regex)$/ } @unfiltered; --Art : -Original Message- : From: Tanya Graham [mailto:[EMAIL PROTECTED]] : Sent: Monday, June 04, 2001 2:16 PM : To: 'Troy Sniff'; [EMAIL PROTECTED] : Subject: RE: grep : : : ok, the grep works when i hardcode it. any ideas on how to take the : extensions from the command line? would the user have to type : it in the : correct format, i.e. (\.c|\.cpp|\.h|\.rc) : thanks : tanya : : -Original Message- : From: Troy Sniff [mailto:[EMAIL PROTECTED]] : Sent: Monday, June 04, 2001 11:19 AM : To: [EMAIL PROTECTED] : Subject: RE: grep : : : Why not put the . before the (). This way if the list of type of files : is long, you don't have to retype the \. every time. : : my @filtered = grep { /\.(html|asp)$/ } @unfiltered; : : Just a thought. : : Troy : : : : -Original Message- : From: [EMAIL PROTECTED] : [mailto:[EMAIL PROTECTED]] On Behalf Of : Arthur Cohen : Sent: Monday, June 04, 2001 11:47 AM : To: Tanya Graham; [EMAIL PROTECTED] : Subject: RE: grep : : : Grep is what you want, e.g. : : : : my @filtered = grep { /(\.html|\.asp)$/ } @unfiltered; : : : : --Art : : : : : -Original Message- : : : From: Tanya Graham [mailto:[EMAIL PROTECTED]] : : : Sent: Monday, June 04, 2001 1:38 PM : : : To: [EMAIL PROTECTED] : : : Subject: grep : : : : : : : : : Hi, : : : I need to be able to take my array of files @files, and : : : exclude files that : : : aren't of a certain extensions. more specifically, one of my : : : arguments on : : : the command line is a comma-separated list of file extensions : : : and i need to : : : alter only the files with those extensions...do i use grep : : : for this? is : : : there a better way? : : : thank you : : : tanya graham : : : : : : -Original Message- : : : From: Troy Sniff [mailto:[EMAIL PROTECTED]] : : : Sent: Monday, June 04, 2001 10:17 AM : : : To: [EMAIL PROTECTED] : : : Subject: Determining memory leak : : : : : : : : : I have a suspicion I have a memory leak in a script I am working on. : : : : : : How can I go about determining the amount of memory the : : : script is using? : : : : : : Is there a module that will report the amount of memory used while : : : executing parts of the script. Maybe reporting the amount : used as it : : : jumps throughout the script and subs? : : : : : : Troy : : : : : : ___ : : : Perl-Win32-Users mailing list : : : [EMAIL PROTECTED] : : : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : : : ___ : : : Perl-Win32-Users mailing list : : : [EMAIL PROTECTED] : : : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : : : : : ___ : Perl-Win32-Users mailing list : [EMAIL PROTECTED] : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : : : ___ : Perl-Win32-Users mailing list : [EMAIL PROTECTED] : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : ___ : Perl-Win32-Users mailing list : [EMAIL PROTECTED] : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: grep
ok, the grep works when i hardcode it. any ideas on how to take the extensions from the command line? would the user have to type it in the correct format, i.e. (\.c|\.cpp|\.h|\.rc) thanks tanya -Original Message- From: Troy Sniff [mailto:[EMAIL PROTECTED]] Sent: Monday, June 04, 2001 11:19 AM To: [EMAIL PROTECTED] Subject: RE: grep Why not put the . before the (). This way if the list of type of files is long, you don't have to retype the \. every time. my @filtered = grep { /\.(html|asp)$/ } @unfiltered; Just a thought. Troy -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Arthur Cohen Sent: Monday, June 04, 2001 11:47 AM To: Tanya Graham; [EMAIL PROTECTED] Subject: RE: grep Grep is what you want, e.g. my @filtered = grep { /(\.html|\.asp)$/ } @unfiltered; --Art : -Original Message- : From: Tanya Graham [mailto:[EMAIL PROTECTED]] : Sent: Monday, June 04, 2001 1:38 PM : To: [EMAIL PROTECTED] : Subject: grep : : : Hi, : I need to be able to take my array of files @files, and : exclude files that : aren't of a certain extensions. more specifically, one of my : arguments on : the command line is a comma-separated list of file extensions : and i need to : alter only the files with those extensions...do i use grep : for this? is : there a better way? : thank you : tanya graham : : -Original Message- : From: Troy Sniff [mailto:[EMAIL PROTECTED]] : Sent: Monday, June 04, 2001 10:17 AM : To: [EMAIL PROTECTED] : Subject: Determining memory leak : : : I have a suspicion I have a memory leak in a script I am working on. : : How can I go about determining the amount of memory the : script is using? : : Is there a module that will report the amount of memory used while : executing parts of the script. Maybe reporting the amount used as it : jumps throughout the script and subs? : : Troy : : ___ : Perl-Win32-Users mailing list : [EMAIL PROTECTED] : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : ___ : Perl-Win32-Users mailing list : [EMAIL PROTECTED] : http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users : ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
i found it...for some reason since i couldn't find "parameters" in the index i freaked out, and after a few minutes i realized i could look up "arguments"...been a long day... tanya -Original Message- From: Trever Furnish [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 2:33 PM To: Tanya Graham; [EMAIL PROTECTED] Subject: RE: directory again Unix: man perlrun Windows: Open the perl documentation and read the section titled "perlrun". Both: Take two aspirin. > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED]]On Behalf Of > Tanya Graham > Sent: Thursday, May 31, 2001 4:02 PM > To: '[EMAIL PROTECTED]' > Subject: RE: directory again > > > Does anyone know where I can find information on passing > parameters (flags) > on the command line? > thank you > tanya > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Thursday, May 31, 2001 9:13 AM > To: '[EMAIL PROTECTED]' > Subject: Re: directory again > > > > >Hi, > >I need to be able to recursively go through a directory and if i > encounter > a > >file folder, perform the same actions on the files in that file folder. > is > >there a simple way to do this, like with an if-statement? > >thanks > >tanya graham > > Tanya, > > Please try to avoid replying with someone else's post, and just changing > the subject, it's confusing. > > Everyone here seems to have gone far out of their way to respond to your > earlier directory dilemma, including me, so allow me to now voice a few > suggestions. > > >From your previous post, and this one I think it's fair to assume you are > very new to perl. The reason I like this mailing list so much is > that they > are very kind to newbies, and I've never seen a flame war on this list. > Being new, it's often easy to not even know where the documentation is. > First, check the FAQ's, they are installed in HTML format when you install > ActiveState perl, many "easy" problems can be resolved right there. Many > people in response to your previous post pointed towards perldoc (perldoc > -f opendir ) as a tool to find information about function and > module usage. > Another tool is PPM, which you can use to search for modules and install > them from ActiveState's (or any other ) ppm repository. Often > it's good to > run a search for what you're looking for through there (such as "dir" or > "file" in your case), install a few "likely" sounding packages > and then run > perldoc on them to see if they offer the features you are really looking > for. You can type "help" in ppm for usage help, and perldoc perldoc will > give you more than you ever wanted to know about how to use perldoc. Also > look at the O'Reilly series of perl books, as they are invaluable > resources. You can find info on them at www.perl.com. I apoligize to all > for this long post, but while I think none of us has a problem helping, I > personally have a problem when someone doesn't help themself first. > > To answer your current question, you may want to take a look at the > File::Find module as this will recurse through a directory tree and can > perform a specified callback (subroutine) on each file it finds. > > Sorry for the long post, > > Chuck > > > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
Does anyone know where I can find information on passing parameters (flags) on the command line? thank you tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 9:13 AM To: '[EMAIL PROTECTED]' Subject: Re: directory again >Hi, >I need to be able to recursively go through a directory and if i encounter a >file folder, perform the same actions on the files in that file folder. is >there a simple way to do this, like with an if-statement? >thanks >tanya graham Tanya, Please try to avoid replying with someone else's post, and just changing the subject, it's confusing. Everyone here seems to have gone far out of their way to respond to your earlier directory dilemma, including me, so allow me to now voice a few suggestions. >From your previous post, and this one I think it's fair to assume you are very new to perl. The reason I like this mailing list so much is that they are very kind to newbies, and I've never seen a flame war on this list. Being new, it's often easy to not even know where the documentation is. First, check the FAQ's, they are installed in HTML format when you install ActiveState perl, many "easy" problems can be resolved right there. Many people in response to your previous post pointed towards perldoc (perldoc -f opendir ) as a tool to find information about function and module usage. Another tool is PPM, which you can use to search for modules and install them from ActiveState's (or any other ) ppm repository. Often it's good to run a search for what you're looking for through there (such as "dir" or "file" in your case), install a few "likely" sounding packages and then run perldoc on them to see if they offer the features you are really looking for. You can type "help" in ppm for usage help, and perldoc perldoc will give you more than you ever wanted to know about how to use perldoc. Also look at the O'Reilly series of perl books, as they are invaluable resources. You can find info on them at www.perl.com. I apoligize to all for this long post, but while I think none of us has a problem helping, I personally have a problem when someone doesn't help themself first. To answer your current question, you may want to take a look at the File::Find module as this will recurse through a directory tree and can perform a specified callback (subroutine) on each file it finds. Sorry for the long post, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory
it appears to work...thanks so much! i'll test it more thoroughly after lunch :) thanks again tanya graham -Original Message- From: Rubinow, Larry [mailto:[EMAIL PROTECTED]] Sent: Wednesday, May 30, 2001 11:59 AM To: 'Tanya Graham'; 'Troy Sniff'; [EMAIL PROTECTED] Subject: RE: directory Tanya Graham wrote: > actually, it was almost perfect before, it would just add teh > copyright > thing a few extra times...sometimes it wouldn't do it at all. > i cant' really > detect a pattern as to which files to which it is doing it > extra...i get teh > infinite loop now that i'm working with the array. that code > is in my last > email. > thanks for any ideas > tanya My bad for not giving you enough code. The problem is that the "next while" line is now referencing the inner while loop, and it gets stuck. You should be able to do it like this: my @files = grep { defined && !/^\./ } readdir (DIR); foreach my $file(@files){ open (SOURCE, "$dirname/$file") or die "can't open original file $file "; # ... ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory
actually, it was almost perfect before, it would just add teh copyright thing a few extra times...sometimes it wouldn't do it at all. i cant' really detect a pattern as to which files to which it is doing it extra...i get teh infinite loop now that i'm working with the array. that code is in my last email. thanks for any ideas tanya -Original Message- From: Troy Sniff [mailto:[EMAIL PROTECTED]] Sent: Wednesday, May 30, 2001 11:46 AM To: [EMAIL PROTECTED] Subject: RE: directory It is looping because of what you said. Windows will place the modified files at the end of the directory. Thus when you modify the last unmodified file, it will continue with already modified files because they were appended to the end of the directory. This can continue forever. Try reading your files into an array and then working with the array. Troy -Original Message----- From: Tanya Graham [mailto:[EMAIL PROTECTED]] Sent: Wednesday, May 30, 2001 12:32 PM To: 'Rubinow, Larry'; Tanya Graham; 'Troy Sniff'; [EMAIL PROTECTED] Subject: RE: directory it looks like it is in an infinite loop now...do my curly braces look wrong? chomp ($dirname = ); opendir (DIR, $dirname) or die "can't open directory $dirname: $!"; $copyright = "COPYRIGHT"; #while ( defined ($file = readdir (DIR))){ my @files = readdir (DIR); foreach my $file(@files){ while (defined $file){ next if ($file=~/^\.+$/); open (SOURCE, "$dirname/$file") or die "can't open original file $file "; open (NEW, ">$dirname/file2.txt") or die "Can't Create new file"; print NEW "$copyright" or die "can't print copyright info to new file"; while (){ print NEW $_; } close NEW; close SOURCE; rename "$dirname/file2.txt", "$dirname/$file"; } } thanks tanya -Original Message- From: Rubinow, Larry [mailto:[EMAIL PROTECTED]] Sent: Wednesday, May 30, 2001 11:24 AM To: 'Tanya Graham'; 'Troy Sniff'; [EMAIL PROTECTED] Subject: RE: directory Tanya Graham wrote: > it is very close...sometimes it adds the copyright twice, it > looks like it > does that to the first and last file in the folder. any ideas > why? also, on Weird. It's possible you're actually reopening a file you've already prepended to. Don't know. To ensure you're not doing this, try reading all the file names at once, rather than one by one; then you don't have to worry about the OS modifying the directory contents while your loop is running. Instead of while ( defined ($file = readdir (DIR))){ try my @files = readdir( DIR ); foreach my $file(@files) { while( defined $file ) { # ... > a larger note, do you think there will be problems when i'm using this > program to alter .c and .cpp files (among others). i'm going to have > to use this program to change this company's code, and i don't know if > having file2.txt will screw things up. > thanks > tanya Your code renames file2.txt in each iteration; it should be gone after the program finishes. In any event, I can't imagine that a .txt file is going to confuse any make environment. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory
i don't think it is finding the files in the directory...when i hardcoded a PRINT to one of the files in the directory it appended it. here's another little problem...the Seek function SEEMS to work (as it doesn't die) but it still appends to the end of the file, not the beginning. (when i take out the seek, it does the same thing as when it was there) any ideas? i have to go sometime in the next few minutes(carpool), but any responses are greatly appreciated, as i will be working on this again bright and early tomorrow morning. thanks again, tanya -Original Message- From: Ron [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 4:33 PM To: perl win32 users Subject: Re: directory Tanya, next if($file =~ /^.+$/);# from Tushar and... next if $file eq '.' or file eq '..'; # from me ... do the same thing. Unless you have a file called '...' or '...' :) Ron - Original Message - From: "Kulkarni, Tushar (GEL, MSX)" <[EMAIL PROTECTED]> To: "'Tanya Graham'" <[EMAIL PROTECTED]>; "'Ron'" <[EMAIL PROTECTED]>; "perl win32 users" <[EMAIL PROTECTED]> Sent: Tuesday, May 29, 2001 7:16 PM Subject: RE: directory > Just put, > > at the start of your while loop. "." and ".." directories are > special directories which refers to current directory and parent directory. > U need to skip this. > Hope this solves ur problem > > Tushar > > -Original Message- > From: Tanya Graham [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 29, 2001 7:03 PM > To: 'Ron'; perl win32 users > Subject: RE: directory > > > ok, for some reason if i hardcode it, it is fine. it won't work if i try and > get it from STDIN. anyway, here is my program: > #!/usr/bin/perl > > $dirname = 'C:/PerlExp'; > opendir (DIR, $dirname) or die "can't open directory > $dirname: $!"; > > while ( defined ($file = readdir (DIR))){ > open (SOURCE, ">>$file") or die "can't open file $file for > appending"; > seek (SOURCE, 0, 0) or die "can't point to beginning"; > print SOURCE "copyright info yada yada yada" ; > close (SOURCE); > } > closedir(DIR); > > i get the error "can't open file . for appending at C:|append.pl line 7" > is it trying to open something called "."? the files in that folder are all > like "tester.txt" "tester2.txt", etc > any ideas? > tanya > > -Original Message- > From: Ron [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 29, 2001 4:12 PM > To: perl win32 users > Subject: Re: directory > > > >From perlport document: > > = > "System calls accept either / or \ as the path separator. > > However, many command?line utilities of DOS vintage treat / as the option > prefix, > so they may get confused by filenames containing /. > Aside from calling any external programs, / will work just fine, and > probably better, > as it is more consistent with popular usage, > and avoids the problem of remembering what to backwhack and what not to." > = > > "System calls" includes opendir(). > > By the way, I tested with both / and \ as file separators in the program I > posted. > > - Original Message - > From: "Tanya Graham" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]>; > <[EMAIL PROTECTED]> > Sent: Tuesday, May 29, 2001 6:31 PM > Subject: RE: directory > > > > it still doesn't work...it says there is no such file or directory...why > > does it use back slashes, but when i specify the path i'm supposed to use > > forward slashes? > > tanya > > > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, May 29, 2001 3:35 PM > > To: [EMAIL PROTECTED] > > Subject: Re: directory > > > > > > > > >Hi, > > >I am trying to open a directory, but I don't know how to specify it > > exactly. > > >I just need to open something in the C drive, under a folder named > > PerlExp. > > >I know this is a dumb question, but does anyone want to help me? > > >thanks > > >tanya graham > > > > Tanya, > > > > You can open directories in a mannor very similar to opening and reading a > > file. > > > > #-- > > > > my $perlexp_dir = "C:/PerlExp"; > > ope
RE: directory
ok, i don't get the "." or ".." error anymore...thanks! however, it doesn't append anything. i don't get any errors and it never dies, it just doesn't do anything. any ideas why? here is my code: #!/usr/bin/perl $dirname = 'C:/PerlExp'; opendir (DIR, $dirname) or die "can't open directory $dirname: $!"; while ( defined ($file = readdir (DIR))){ next if($file =~ /^.+$/); open (SOURCE, ">>$dirname/$file") or die "can't open file $file for appending"; seek (SOURCE, 0, 0) or die "can't point to beginning"; print SOURCE "copyright info yada yada yada" or die "can't print"; } close (SOURCE); closedir(DIR); thanks tanya -Original Message- From: Ron [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 4:33 PM To: perl win32 users Subject: Re: directory Tanya, next if($file =~ /^.+$/);# from Tushar and... next if $file eq '.' or file eq '..'; # from me ... do the same thing. Unless you have a file called '...' or '...' :) Ron - Original Message - From: "Kulkarni, Tushar (GEL, MSX)" <[EMAIL PROTECTED]> To: "'Tanya Graham'" <[EMAIL PROTECTED]>; "'Ron'" <[EMAIL PROTECTED]>; "perl win32 users" <[EMAIL PROTECTED]> Sent: Tuesday, May 29, 2001 7:16 PM Subject: RE: directory > Just put, > > at the start of your while loop. "." and ".." directories are > special directories which refers to current directory and parent directory. > U need to skip this. > Hope this solves ur problem > > Tushar > > -Original Message- > From: Tanya Graham [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 29, 2001 7:03 PM > To: 'Ron'; perl win32 users > Subject: RE: directory > > > ok, for some reason if i hardcode it, it is fine. it won't work if i try and > get it from STDIN. anyway, here is my program: > #!/usr/bin/perl > > $dirname = 'C:/PerlExp'; > opendir (DIR, $dirname) or die "can't open directory > $dirname: $!"; > > while ( defined ($file = readdir (DIR))){ > open (SOURCE, ">>$file") or die "can't open file $file for > appending"; > seek (SOURCE, 0, 0) or die "can't point to beginning"; > print SOURCE "copyright info yada yada yada" ; > close (SOURCE); > } > closedir(DIR); > > i get the error "can't open file . for appending at C:|append.pl line 7" > is it trying to open something called "."? the files in that folder are all > like "tester.txt" "tester2.txt", etc > any ideas? > tanya > > -Original Message- > From: Ron [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 29, 2001 4:12 PM > To: perl win32 users > Subject: Re: directory > > > >From perlport document: > > = > "System calls accept either / or \ as the path separator. > > However, many command?line utilities of DOS vintage treat / as the option > prefix, > so they may get confused by filenames containing /. > Aside from calling any external programs, / will work just fine, and > probably better, > as it is more consistent with popular usage, > and avoids the problem of remembering what to backwhack and what not to." > = > > "System calls" includes opendir(). > > By the way, I tested with both / and \ as file separators in the program I > posted. > > - Original Message - > From: "Tanya Graham" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]>; > <[EMAIL PROTECTED]> > Sent: Tuesday, May 29, 2001 6:31 PM > Subject: RE: directory > > > > it still doesn't work...it says there is no such file or directory...why > > does it use back slashes, but when i specify the path i'm supposed to use > > forward slashes? > > tanya > > > > -Original Message- > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > > Sent: Tuesday, May 29, 2001 3:35 PM > > To: [EMAIL PROTECTED] > > Subject: Re: directory > > > > > > > > >Hi, > > >I am trying to open a directory, but I don't know how to specify it > > exactly. > > >I just need to open something in the C drive, under a folder named > > PerlExp. > > >I know this is a dumb question, but does anyone want to help me? > > >thanks > > >tanya graham
RE: directory
ok, for some reason if i hardcode it, it is fine. it won't work if i try and get it from STDIN. anyway, here is my program: #!/usr/bin/perl $dirname = 'C:/PerlExp'; opendir (DIR, $dirname) or die "can't open directory $dirname: $!"; while ( defined ($file = readdir (DIR))){ open (SOURCE, ">>$file")or die "can't open file $file for appending"; seek (SOURCE, 0, 0) or die "can't point to beginning"; print SOURCE "copyright info yada yada yada" ; close (SOURCE); } closedir(DIR); i get the error "can't open file . for appending at C:|append.pl line 7" is it trying to open something called "."? the files in that folder are all like "tester.txt" "tester2.txt", etc any ideas? tanya -Original Message- From: Ron [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 4:12 PM To: perl win32 users Subject: Re: directory >From perlport document: = "System calls accept either / or \ as the path separator. However, many command?line utilities of DOS vintage treat / as the option prefix, so they may get confused by filenames containing /. Aside from calling any external programs, / will work just fine, and probably better, as it is more consistent with popular usage, and avoids the problem of remembering what to backwhack and what not to." = "System calls" includes opendir(). By the way, I tested with both / and \ as file separators in the program I posted. - Original Message - From: "Tanya Graham" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Tuesday, May 29, 2001 6:31 PM Subject: RE: directory > it still doesn't work...it says there is no such file or directory...why > does it use back slashes, but when i specify the path i'm supposed to use > forward slashes? > tanya > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, May 29, 2001 3:35 PM > To: [EMAIL PROTECTED] > Subject: Re: directory > > > > >Hi, > >I am trying to open a directory, but I don't know how to specify it > exactly. > >I just need to open something in the C drive, under a folder named > PerlExp. > >I know this is a dumb question, but does anyone want to help me? > >thanks > >tanya graham > > Tanya, > > You can open directories in a mannor very similar to opening and reading a > file. > > #-- > > my $perlexp_dir = "C:/PerlExp"; > opendir( DIRHNDLE, $perlexp_dir ) or die "Failed to open $perlexp_dir!"; > > # if opendir works you now have a handle into the directory > # stored in DIRHNDLE. There are many ways to "work" with it > > # one way > my @file_list = readdir DIRHNDLE; > # now operate on the file_list array > > # another way > while ( defined( my $file = readdir DIRHNDLE ) ) { > # operate on $file > } > > # another way > for my $file ( readdir DIRHNDLE ) { > # operate on $file > } > > closedir( DIRHNDLE ) or die "Failed to close $perlexp_dir!"; > > #-- > > Remember pick ONE of the methods above as once you've "used up" the handle > you need to close and re-open it to reset the cursor position back to the > top! The first method is best if you're going to need to make more than > one pass (for the reason just stated) through the data. Open the handle, > read it into an array, and then you can close it immediately. Now you can > rip through the array as many times as needed without having to pay any IO > penalties. The while and for examples are handy for when you know you only > need to make a single pass through the file listing. > > HTH, > > Chuck > > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory
it still doesn't work...it says there is no such file or directory...why does it use back slashes, but when i specify the path i'm supposed to use forward slashes? tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 3:35 PM To: [EMAIL PROTECTED] Subject: Re: directory >Hi, >I am trying to open a directory, but I don't know how to specify it exactly. >I just need to open something in the C drive, under a folder named PerlExp. >I know this is a dumb question, but does anyone want to help me? >thanks >tanya graham Tanya, You can open directories in a mannor very similar to opening and reading a file. #-- my $perlexp_dir = "C:/PerlExp"; opendir( DIRHNDLE, $perlexp_dir ) or die "Failed to open $perlexp_dir!"; # if opendir works you now have a handle into the directory # stored in DIRHNDLE. There are many ways to "work" with it # one way my @file_list = readdir DIRHNDLE; # now operate on the file_list array # another way while ( defined( my $file = readdir DIRHNDLE ) ) { # operate on $file } # another way for my $file ( readdir DIRHNDLE ) { # operate on $file } closedir( DIRHNDLE ) or die "Failed to close $perlexp_dir!"; #-- Remember pick ONE of the methods above as once you've "used up" the handle you need to close and re-open it to reset the cursor position back to the top! The first method is best if you're going to need to make more than one pass (for the reason just stated) through the data. Open the handle, read it into an array, and then you can close it immediately. Now you can rip through the array as many times as needed without having to pay any IO penalties. The while and for examples are handy for when you know you only need to make a single pass through the file listing. HTH, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
directory
Hi, I am trying to open a directory, but I don't know how to specify it exactly. I just need to open something in the C drive, under a folder named PerlExp. I know this is a dumb question, but does anyone want to help me? thanks tanya graham -Original Message- From: Purcell, Scott [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 2:20 PM To: 'Dan Jablonsky'; [EMAIL PROTECTED] Subject: RE: remove duplicate lines I am not an expert here, but why compare each line. Read each line into a hash, whereas each line becomes a key, and set the value to something like a 1, and your dups would be gone, quickly and I believe pretty efficiently. eg. my %textHash = (); open (SOMEDATAFILE, "<$pathToFile/theFile") or die("Can't open. $!") while () { next if ($_ =~ /^\s+$/); # possibly skip blank lines and any ohter stuff in yoiur files. chomp; # remove newline $texthash{$_} = 1; } Just a thought, Scott -Original Message- From: Dan Jablonsky [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 29, 2001 4:09 PM To: [EMAIL PROTECTED] Subject: remove duplicate lines Hi all, I need to remove duplicate lines from a whole bunch of files, I already have a script that does this but it's brute force (compare first line with the others one by one; if no match write it to another file), hence very inefficient. Somehow I believe there must be a nicer approach. Does anybody have a script that does that in an elegant/efficient manner? Thanks a lot, Dan __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users