Ask a Question related to PERL Miscellaneous, Design and Development.
-
Kubaton Lover #1
Writing out part of a file
This is a newbie question I'm sure, but I couldn't find a "good" way to
handle this problem.
I have an XML file that is being read by a Perl script. The fact that it is
XML is superfluous.
I want to open the file, print the first 2 lines of the file out, then print
out a line or two of something else, then print out the remaining portion of
the file.
I tried:
while (<CODE>) {
print $_;
last;
}
print "my stuff here\n";
while (<CODE>) {
print $_;
}
This works fine for printing just the first line of the file and I figure
that I could add a counter and break out when that counter reaches a certain
number, but is there a "better" (more efficient) way to accomplish this
task?
Thanks.
Kubaton Lover Guest
-
Writing a text file to the file system
Using Visual Studio C# When I ran the following code: System.IO; private void Button1_Click(object sender, System.EventArgs e) {... -
Error: Could not find a part of the path / Reading and Writing to files in ASP.Net
Hello, I'm trying to read a text file located in the top folder of the virtual directory and I'm receiving the following error: "Could not... -
Setting the file permissions of a file I'm writing to
Is it possible to specify the permissions of a file I create when I: open ("FOO", "> ./bar") or die ("Could not create file"); Thanks in... -
File Access error - writing to .txt file
Normally web sites run under the ASPNET user account. It appears that this account does not have write privileges to the file path you've... -
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY?
Hi. This is an error that comes up fairly regularly when trying to run the "Rebuild All" command in a Solution that contains more than one... -
J. Gleixner #2
Re: Writing out part of a file
Kubaton Lover wrote:
open (F, "<file") || die "$!";> This is a newbie question I'm sure, but I couldn't find a "good" way to
> handle this problem.
>
> I have an XML file that is being read by a Perl script. The fact that it is
> XML is superfluous.
>
> I want to open the file, print the first 2 lines of the file out, then print
> out a line or two of something else, then print out the remaining portion of
> the file.
>
> I tried:
>
> while (<CODE>) {
> print $_;
> last;
> }
> print "my stuff here\n";
> while (<CODE>) {
> print $_;
> }
>
> This works fine for printing just the first line of the file and I figure
> that I could add a counter and break out when that counter reaches a certain
> number, but is there a "better" (more efficient) way to accomplish this
> task?
>
> Thanks.
>
>
print "First=",scalar <F>; #or into a variable .. my $l1 = <F>;
print "Second=", scalar <F>;
print "Rest is:\n";
while (<F>)
{
print;
}
close F;
Or, if your file is fairly small, just slurp it all into an array then
print the various indexes of the array.
J. Gleixner Guest
-
Tad McClellan #3
Re: Writing out part of a file
Kubaton Lover <nospam@goawayspam.com> wrote:
> I want to open the file, print the first 2 lines of the file out, then print
> out a line or two of something else, then print out the remaining portion of
> the file.
# untested
print scalar <CODE>;
print scalar <CODE>;
print "my stuff here\n";
print while <CODE>;
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Kubaton Lover #4
Re: Writing out part of a file
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnbmm85s.gk0.tadmc@magna.augustmail.com...> Kubaton Lover <nospam@goawayspam.com> wrote:
>> > I want to open the file, print the first 2 lines of the file out, thenportion of> > out a line or two of something else, then print out the remainingPoifect! I knew it had to be easier than I was making it.>> > the file.
>
> # untested
> print scalar <CODE>;
> print scalar <CODE>;
> print "my stuff here\n";
> print while <CODE>;
>
>
> --
> Tad McClellan SGML consulting
> [email]tadmc@augustmail.com[/email] Perl programming
> Fort Worth, Texas
Kubaton Lover Guest
-
Anno Siegel #5
Re: Writing out part of a file
Tad McClellan <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
If the CODE file doesn't have two lines, this runs a bit bumpy.> Kubaton Lover <nospam@goawayspam.com> wrote:
>>> > I want to open the file, print the first 2 lines of the file out, then print
> > out a line or two of something else, then print out the remaining portion of
> > the file.
>
> # untested
> print scalar <CODE>;
> print scalar <CODE>;
> print "my stuff here\n";
> print while <CODE>;
I actually liked the OPs solution just fine. Using a while-loop to print
a fixed number of lines may look like overkill, but it handles marginal
cases better. Considering that $. is already there to count input lines,
it becomes
while ( <CODE> ) {
print;
last if $. == 2;
}
print "'***my stuff***^\n";
print while <CODE>;
That's what I would use.
Anno
Anno Siegel Guest



Reply With Quote

