Ask a Question related to PERL Miscellaneous, Design and Development.
-
MJS #1
Saving entire file
Can someone help me with the following code.
It just saves/prints the line in which 'hello' occured.
why doesn't it save the entire file i.e why don't i get the entire
content of file1 in file2 ?
open(FILEREAD, "file1") or die "Can't open file1: $!\n";
while(<FILEREAD>) {
#searching the string
if(/hello/) {
open(FILEWRITE, ">file2") or die "Can't open file2: $!\n";
select FILEWRITE; # this is not needed.
print FILEWRITE ;
close(FILEWRITE);
}
}
close(FILEREAD);
MJS Guest
-
How do I convert an entire xls spreadsheet into only 1 pdf file?
How do I convert an entire xls spreadsheet into 1 pdf file. It will only convert one sheet per pdf file. -
extracting an entire node from XML file
i have a file which i have to write in C#. i want to extract an entire node by itself at once ( wherever it occurs in the file .. as ) <?xml... -
[PHP] Value for entire file
> -----Original Message----- No, $_GET has always been superglobal, it just didn't exist before 4.1.0 -- so for PHP <4.1.0 it's: function... -
Value for entire file
Hello, I am facing one problem..i am getting one variable value from another file by using href..I need to use that value in the entire file..how... -
[PHP] Value for entire file
On Friday 18 July 2003 12:40, Uma Shankari T. wrote: You should be using correctly formed HTML: <a href="..." xxx="..."> echo $_GET; //... -
Mike Flannigan #2
Re: Saving entire file
Gunnar Hjalmarsson wrote:
If I'm not mistaken, you also need to open the filewrite for> MJS wrote:>> > Can someone help me with the following code.
> > It just saves/prints the line in which 'hello' occured.
> > why doesn't it save the entire file i.e why don't i get the entire
> > content of file1 in file2 ?
> Because you didn't tell Perl to do that. :)
>>> > print FILEWRITE ;
> That prints the content of $_ to FILEWRITE. Please study
> [url]http://www.perldoc.com/perl5.8.0/pod/perlsyn.html[/url]
>
> If you replace that line with:
>
> seek FILEREAD, 0, 0;
> print FILEWRITE join '', <FILEREAD>;
>
> the script should do what you want. You may want to reed about 'seek'
> and 'join' at [url]http://www.perldoc.com/perl5.8.0/pod/perlfunc.html[/url]
>
> There are (of course) other available solutions...
appending.
open(FILEWRITE, ">>file2") or die "Can't open file2: $!\n";
^
^
Mike Flannigan Guest
-
Gunnar Hjalmarsson #3
Re: Saving entire file
Mike Flannigan wrote:
Only if appending is what OP wants to do. Note that it was opened for> Gunnar Hjalmarsson wrote:>>>MJS wrote:
>>>>>>>Can someone help me with the following code.
>>>It just saves/prints the line in which 'hello' occured.
>>>why doesn't it save the entire file i.e why don't i get the entire
>>>content of file1 in file2 ?
>>Because you didn't tell Perl to do that. :)
>>>>>>>print FILEWRITE ;
>>That prints the content of $_ to FILEWRITE. Please study
>>[url]http://www.perldoc.com/perl5.8.0/pod/perlsyn.html[/url]
>>
>>If you replace that line with:
>>
>> seek FILEREAD, 0, 0;
>> print FILEWRITE join '', <FILEREAD>;
>>
>>the script should do what you want. You may want to reed about 'seek'
>>and 'join' at [url]http://www.perldoc.com/perl5.8.0/pod/perlfunc.html[/url]
>>
>>There are (of course) other available solutions...
> If I'm not mistaken, you also need to open the filewrite for
> appending.
>
> open(FILEWRITE, ">>file2") or die "Can't open file2: $!\n";
writing.
But I'd like to correct my solution in another respect. The 'join'
function is redundant, and the following should do it:
seek FILEREAD, 0, 0;
print FILEWRITE <FILEREAD>;
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
ctcgag@hotmail.com #4
Re: Saving entire file
[email]tabletdesktop@yahoo.com[/email] (MJS) wrote:
For every line in file1 containing hello, you write that one line> Can someone help me with the following code.
> It just saves/prints the line in which 'hello' occured.
> why doesn't it save the entire file i.e why don't i get the entire
> content of file1 in file2 ?
>
> open(FILEREAD, "file1") or die "Can't open file1: $!\n";
>
> while(<FILEREAD>) {
> #searching the string
> if(/hello/) {
>
> open(FILEWRITE, ">file2") or die "Can't open file2: $!\n";
> select FILEWRITE; # this is not needed.
> print FILEWRITE ;
> close(FILEWRITE);
> }
> }
> close(FILEREAD);
to file2, overwriting whatever file2 may have existed previously.
Thus, the eventual file2 is left with the final hello containing
line in file1.
--
-------------------- [url]http://NewsReader.Com/[/url] --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
ctcgag@hotmail.com Guest



Reply With Quote

