🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.perl.misc
5 messages
5 total messages Started by Zenin Wed, 06 Dec 1995 00:00
Writing to file?
#46
Author: Zenin
Date: Wed, 06 Dec 1995 00:00
19 lines
910 bytes
I can't seem to figure out how to write to a file.
I thought it was somthing like
	write(FOO, "filename");

but I guess it's no so easy. :(

So what is the syntax to do somthing like:
	#!/usr/bin/perl
	open(FOO, "original_filename");
	while(<FOO>) {
		do some stuff;
	}
	write(FOO, "new_filename");
	close FOO;

TIA!

-Zenin
zenin@best.com
Re: Writing to file?
#47
Author: Laird Nelson
Date: Thu, 07 Dec 1995 00:00
47 lines
2288 bytes
Zenin (zenin@best.com) wrote:
> I can't seem to figure out how to write to a file.
> I thought it was somthing like
> 	write(FOO, "filename");
> but I guess it's no so easy. :(

Have you grepped the perlfunc manpage for "open"?  This is a basic
usage of said function.

You open a file for reading by prefixing the filename with a "<" (or
nothing; it opens for reading by default), and for writing by
prefixing the filename with a ">".  I guarantee you will write several
scripts in which you open one file for reading, and hope to open
another for writing, but you'll forget the ">", and you'll spend at
least four hours wondering what the hell is the matter.  :)

This program, if I have my syntax right, opens $infile for reading,
$outfile for writing, and, for each line in $infile, writes the
following string to $outfile: "foobar.txt's line [whatever the line
number is] contains: [contents of current line from foobar.txt]".

I heartily recommend reading the perlfunc manpage, followed by Tom
Christiansen's FAQ.  That is, you are nuts if you don't.  :)

#!/usr/local/bin/perl -w

$infile = "foobar.txt"; # or whatever
$outfile = "snarfblat.txt"; # or whatever

open (INFILE, "<$infile");
open (OUTFILE, ">$outfile");

while (<INFILE>) {

    print OUTFILE "$infile's line $. contains: $_";

}

__END__



--
laird j. nelson                                           (617) 252-5147
at&t interchange online network                           25 first street
http://www.amherst.edu/~ljnelson/                         cambridge, ma 02143
    I do not speak for my employer, and my employer does not speak for me.
Re: Writing to file?
#48
Author: Rodrigo Vanegas
Date: Sat, 09 Dec 1995 00:00
34 lines
1471 bytes
In article <4a54h9$a0g@shellx.best.com> Zenin <zenin@best.com> writes:

> I can't seem to figure out how to write to a file.
> I thought it was somthing like
> 	write(FOO, "filename");
> 
> but I guess it's no so easy. :(
> 
> So what is the syntax to do somthing like:
> 	#!/usr/bin/perl
> 	open(FOO, "original_filename");
> 	while(<FOO>) {
> 		do some stuff;
> 	}
> 	write(FOO, "new_filename");
> 	close FOO;
> 

The FOO is a filehandle associated with the open file.  So for
starters, I don't think you want to use FOO for both files.  What is
it you want to write to "new_filename" anyway?  It's clear from your
code above.

Here's how to read a line from one file and write it to another.

  open(FILE1,"one");
  $line = <FILE1>;
  open(FILE2,"> another");
  print FILE2 $line;
  close(FILE1);
  close(FILE2);

-- 
Rodrigo <Vanegas@bbn.com>
Re: Writing to file?
#49
Author: Jon Orwant
Date: Mon, 11 Dec 1995 00:00
44 lines
1672 bytes
In article <4a54h9$a0g@shellx.best.com> Zenin <zenin@best.com> writes:

   I can't seem to figure out how to write to a file.
   I thought it was somthing like
	   write(FOO, "filename");

   but I guess it's no so easy. :(

Oh, it is that easy.  But you don't want write()---you want print().
write() is for strange beasts called formats.

   So what is the syntax to do somthing like:
	   #!/usr/bin/perl
	   open(FOO, "original_filename");
	   while(<FOO>) {
		   do some stuff;
	   }
	   write(FOO, "new_filename");
	   close FOO;


#!/usr/bin/perl

open (FOO, "original_filename");
while (<FOO>) {
	do some(stuff);
}

open (FOO, ">new_filename");     # open new_filename for writing,
                                 # implicitly closing the old FOO
print FOO "some new text\n";     

close FOO;                       # not necessary, but good style.



To open FOO for both reading and writing, you can use
open (FOO, "<+filename");



Jon Orwant
MIT Media Lab
Re: Writing to file?
#50
Author: Rick Trankle
Date: Mon, 11 Dec 1995 00:00
25 lines
1026 bytes
try this:

print (FOO, "filename")

Zenin <zenin@best.com> wrote:

>I can't seem to figure out how to write to a file.
>I thought it was somthing like
>	write(FOO, "filename");

>but I guess it's no so easy. :(

>So what is the syntax to do somthing like:
>	#!/usr/bin/perl
>	open(FOO, "original_filename");
>	while(<FOO>) {
>		do some stuff;
>	}
>	write(FOO, "new_filename");
>	close FOO;

>TIA!

>-Zenin
>zenin@best.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads