Re: write new file to same dir

2005-02-11 Thread Gareth Johnston
On Fri, February 11, 2005 15:45, Brian Volk said:
> Hi All,
>
> I'm having trouble w/ my script... I can open the dir, read the dir and
even
> get the s/// to work I just don't know how to write the new file to
a
> new dir.   or the same dir for that matter...  If someone could
point
> me
> in the right direction I would really appreciate it.  Maybe a page in
the
> Lama book...  :~)
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $dir = "C:/brian/test_html";
>  opendir (HTML, $dir) or die "Can't open $dir: $!";
> #my $newdir ="C:/brian/test_html_1";
> # opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
>
> # load @ARGV for <> operator below
>
> @ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
>
> while (<>) {
>  chomp;
> s/31990/31720/g;
>  print;
>}
>
>
> closedir (HTML);
>
> ___END
>
>
> Brian Volk
> HP Products
> 317.298.9950 x1245
>   [EMAIL PROTECTED]
>
>
>
Brian, just a beginner myself, but i think the problem lies in your print
going to STDIN by default. You must select the filehandle you want to
print to before printing. Each print following this will print to that
handle so it needs a further change if you want to print elsewhere after
that also.

 while (<>) {
  chomp;
  s/31990/31720/g;
  SELECT HTML1;
  print;
}


Chapter 11: Filehandles, in the Lama Book should help also.

HTH


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: write new file to same dir

2005-02-11 Thread bright true
Hi , 
actually you're just opening a directory ... and you're not writing into a file 
to write a file .. you can use the following 
1- with replace the old file 
open(FILE,">$dir/filenamt.txt");
print FILE "Something Here";
close(FILE);
2- to write into  a file with out replace it .. (with out removing the old one).
open(FILE,">>$dir/filename.txt");
print FILE "Something to print in the file";
close(FILE);

i hope i helped you :)

bye

On Fri, 11 Feb 2005 10:45:40 -0500, Brian Volk <[EMAIL PROTECTED]> wrote:
> Hi All,
> 
> I'm having trouble w/ my script... I can open the dir, read the dir and even
> get the s/// to work I just don't know how to write the new file to a
> new dir.   or the same dir for that matter...  If someone could point me
> in the right direction I would really appreciate it.  Maybe a page in the
> Lama book...  :~)
> 
> #!/usr/local/bin/perl
> 
> use strict;
> use warnings;
> 
> my $dir = "C:/brian/test_html";
> opendir (HTML, $dir) or die "Can't open $dir: $!";
> #my $newdir ="C:/brian/test_html_1";
> # opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
> 
> # load @ARGV for <> operator below
> 
> @ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
> 
> while (<>) {
> chomp;
>s/31990/31720/g;
> print;
>   }
> 
> closedir (HTML);
> 
> ___END
> 
> 
> Brian Volk
> HP Products
> 317.298.9950 x1245
>  [EMAIL PROTECTED]
> 
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: write new file to same dir

2005-02-11 Thread Brian Volk
Thank you for your help... Where I'm still confused is, the script below
does just what I want it to do and I can even see the results in the
STOUT...  I'm just not sure how I get the changes written to the 5 files in
the dir...  

my $dir = "C:/brian/test_html";
opendir (HTML, $dir) or die "Can't open $dir: $!";

Do I change opendir to (HTML, ">>$dir/*.html)   

That didn't seem to work...  Thanks again for your help!

Brian 

> -Original Message-
> From: bright true [mailto:[EMAIL PROTECTED]
> Sent: Friday, February 11, 2005 12:27 PM
> To: Brian Volk
> Cc: Beginners (E-mail)
> Subject: Re: write new file to same dir
> 
> 
> Hi , 
> actually you're just opening a directory ... and you're not 
> writing into a file 
> to write a file .. you can use the following 
> 1- with replace the old file 
> open(FILE,">$dir/filenamt.txt");
> print FILE "Something Here";
> close(FILE);
> 2- to write into  a file with out replace it .. (with out 
> removing the old one).
> open(FILE,">>$dir/filename.txt");
> print FILE "Something to print in the file";
> close(FILE);
> 
> i hope i helped you :)
> 
> bye
> 
> On Fri, 11 Feb 2005 10:45:40 -0500, Brian Volk 
> <[EMAIL PROTECTED]> wrote:
> > Hi All,
> > 
> > I'm having trouble w/ my script... I can open the dir, read 
> the dir and even
> > get the s/// to work I just don't know how to write the 
> new file to a
> > new dir.   or the same dir for that matter...  If 
> someone could point me
> > in the right direction I would really appreciate it.  Maybe 
> a page in the
> > Lama book...  :~)
> > 
> > #!/usr/local/bin/perl
> > 
> > use strict;
> > use warnings;
> > 
> > my $dir = "C:/brian/test_html";
> > opendir (HTML, $dir) or die "Can't open $dir: $!";
> > #my $newdir ="C:/brian/test_html_1";
> > # opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
> > 
> > # load @ARGV for <> operator below
> > 
> > @ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
> > 
> > while (<>) {
> > chomp;
> >s/31990/31720/g;
> > print;
> >   }
> > 
> > closedir (HTML);
> > 
> > ___END
> > 
> > 
> > Brian Volk
> > HP Products
> > 317.298.9950 x1245
> > <mailto:[EMAIL PROTECTED]> [EMAIL PROTECTED]
> > 
> >
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: write new file to same dir

2005-02-11 Thread Wiggins d'Anconia
Brian Volk wrote:
Hi All, 
 
I'm having trouble w/ my script... I can open the dir, read the dir and even
get the s/// to work I just don't know how to write the new file to a
new dir.   or the same dir for that matter...  If someone could point me
in the right direction I would really appreciate it.  Maybe a page in the
Lama book...  :~)

For now I will be (and you should consider) being very explicit. It will 
help you learn and see what is going on, rather than using Perl's 
shortcuts. Until you understand them they are really just rope to hang 
yourself with.

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
Great start!
my $dir = "C:/brian/test_html";
 opendir (HTML, $dir) or die "Can't open $dir: $!";
#my $newdir ="C:/brian/test_html_1";
# opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
 
Normally you would not need to open a directory just to open a file for 
reading/writing, but in this case you clearly want a list of files so 
your opendir is the way to go.

# load @ARGV for <> operator below 
 
@ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
 
Perl lets us make our own variables so lets do that instead,
my @files = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
Additionally since we don't need the directory for anything else we can 
close it now rather than after the loop.

while (<>) {
 chomp;
s/31990/31720/g;
 print; 
   }
Now we have a list of files, loop over them, open them, read and write 
to them, and then close them. Note that we can't open a file for read 
and write at the same time (well not easily).

-- Untested --
foreach my $file (@files) {
  my $READHANDLE;
  unless (open $READHANDLE, "$file") {
 print STDERR "Couldn't open $file for reading: $!";
 next;
  }
  my $WRITEHANDLE;
  unless (open $WRITEHANDLE, ">$file.tmp") {
 print STDERR "Couldn't open $file.tmp for writing: $!";
 next;
  }
  while (my $line = <$READHANDLE>) {
 $line =~ s/31990/31720/g;
 print $WRITEHANDLE $line;
  }
  close $WRITEHANDLE;
  close $READHANDLE;
  # now move our temp file over top of the original
  rename "$file.tmp", $file or die "Can't rename temp file to original: 
$!";
}


 
closedir (HTML);
 
___END

perldoc -f open
perldoc perlopentut
perldoc -f rename
perldoc -f print
Of course then there is the easy, quick, safe way,
perldoc Tie::File
But ties are sometimes difficult to get your head around.
   
Brian Volk
HP Products
317.298.9950 x1245
  [EMAIL PROTECTED]
 
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: write new file to same dir

2005-02-11 Thread John W. Krahn
Brian Volk wrote:
Hi All, 
Hello,
I'm having trouble w/ my script... I can open the dir, read the dir and even
get the s/// to work I just don't know how to write the new file to a
new dir.   or the same dir for that matter...  If someone could point me
in the right direction I would really appreciate it.  Maybe a page in the
Lama book...  :~)
What you are looking for is the "in-place edit" variable/command line switch. 
 The variable is described in the perlvar.pod document and the command line 
switch is described in the perlrun.pod document.

perldoc perlrun
perldoc perlvar

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
my $dir = "C:/brian/test_html";
 opendir (HTML, $dir) or die "Can't open $dir: $!";
#my $newdir ="C:/brian/test_html_1";
# opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
 
# load @ARGV for <> operator below 
 
@ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
$^I = "$newdir/*";

while (<>) {
 chomp;
s/31990/31720/g;
 print; 
   }

 
closedir (HTML);

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: write new file to same dir

2005-02-11 Thread Brian Volk
Thank you so much for spelling it out for me.  I learned a lot!  ...BTW your
"--Untested--" worked great! :~)

Brian 

> -Original Message-
> From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED]
> Sent: Friday, February 11, 2005 1:31 PM
> To: Brian Volk
> Cc: Beginners (E-mail)
> Subject: Re: write new file to same dir
> 
> 
> Brian Volk wrote:
> > Hi All, 
> >  
> > I'm having trouble w/ my script... I can open the dir, read 
> the dir and even
> > get the s/// to work I just don't know how to write the 
> new file to a
> > new dir.   or the same dir for that matter...  If 
> someone could point me
> > in the right direction I would really appreciate it.  Maybe 
> a page in the
> > Lama book...  :~)
> >
> 
> For now I will be (and you should consider) being very 
> explicit. It will 
> help you learn and see what is going on, rather than using Perl's 
> shortcuts. Until you understand them they are really just 
> rope to hang 
> yourself with.
> 
> > #!/usr/local/bin/perl
> >  
> > use strict;
> > use warnings;
> >  
> Great start!
> 
> > my $dir = "C:/brian/test_html";
> >  opendir (HTML, $dir) or die "Can't open $dir: $!";
> > #my $newdir ="C:/brian/test_html_1";
> > # opendir (HTML1, $newdir) or die "Can't open $newdir: $!";
> >  
> 
> Normally you would not need to open a directory just to open 
> a file for 
> reading/writing, but in this case you clearly want a list of files so 
> your opendir is the way to go.
> 
> > # load @ARGV for <> operator below 
> >  
> > @ARGV = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
> >  
> 
> Perl lets us make our own variables so lets do that instead,
> 
> my @files = map { "$dir/$_" } grep { !/^\./ } readdir HTML;
> 
> Additionally since we don't need the directory for anything 
> else we can 
> close it now rather than after the loop.
> 
> > while (<>) {
> >  chomp;
> > s/31990/31720/g;
> >  print; 
> >}
> 
> Now we have a list of files, loop over them, open them, read 
> and write 
> to them, and then close them. Note that we can't open a file for read 
> and write at the same time (well not easily).
> 
> -- Untested --
> foreach my $file (@files) {
>my $READHANDLE;
>unless (open $READHANDLE, "$file") {
>   print STDERR "Couldn't open $file for reading: $!";
>   next;
>}
>my $WRITEHANDLE;
>unless (open $WRITEHANDLE, ">$file.tmp") {
>   print STDERR "Couldn't open $file.tmp for writing: $!";
>   next;
>}
>while (my $line = <$READHANDLE>) {
>   $line =~ s/31990/31720/g;
>   print $WRITEHANDLE $line;
>}
>close $WRITEHANDLE;
>close $READHANDLE;
> 
># now move our temp file over top of the original
>rename "$file.tmp", $file or die "Can't rename temp file 
> to original: 
> $!";
> }
> 
> > 
> >  
> > closedir (HTML);
> >  
> > ___END
> >
> 
> perldoc -f open
> perldoc perlopentut
> perldoc -f rename
> perldoc -f print
> 
> Of course then there is the easy, quick, safe way,
> 
> perldoc Tie::File
> 
> But ties are sometimes difficult to get your head around.
> 
> >
> > Brian Volk
> > HP Products
> > 317.298.9950 x1245
> >  <mailto:[EMAIL PROTECTED]> [EMAIL PROTECTED]
> >  
> 
> http://danconia.org
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>