Writing out part of a file

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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) {...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Writing out part of a file

    Kubaton Lover wrote:
    > 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.
    >
    >
    open (F, "<file") || die "$!";
    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

  4. #3

    Default 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

  5. #4

    Default 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, 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
    Poifect! I knew it had to be easier than I was making it.


    Kubaton Lover Guest

  6. #5

    Default Re: Writing out part of a file

    Tad McClellan <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
    > 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>;
    If the CODE file doesn't have two lines, this runs a bit bumpy.

    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139