Loading ...
Sorry, an error occurred while loading the content.

Writing to the middle of a file.

Expand Messages
  • Jon Morris
    How can I write a line of text to the middle of a file? From what I understand I need to push the file to an array, modify the array and then write back to a
    Message 1 of 8 , Dec 10, 2004
    • 0 Attachment
      How can I write a line of text to the middle of a file? From what I understand I need to push the file to an array, modify the array and then write back to a new file?

      If this is the easiest way of accomplishing this, how would I run through an array until I find a match, insert my string after that match, and push all the other data down one index?

      Thanks,
      Jon
    • Paul Archer
      ... It depends a bit on the size of the file, but that s not a bad way to do it. You could also read in a line at a time, writing back out to your destination
      Message 2 of 8 , Dec 10, 2004
      • 0 Attachment
        2:46pm, Jon Morris wrote:

        > How can I write a line of text to the middle of a file? From what I
        > understand I need to push the file to an array, modify the array and then
        > write back to a new file?

        It depends a bit on the size of the file, but that's not a bad way to do it.
        You could also read in a line at a time, writing back out to your
        destination file. When you hit your target line, write your new line out to
        your destination file. That's better for really large files. (You don't
        wanna read in a multi-megabyte file into memory if you don't have to.)


        > If this is the easiest way of accomplishing this, how would I run through
        > an array until I find a match, insert my string after that match, and push
        > all the other data down one index?
        >
        Pretty much, except you don't have to push the data down the array--Perl
        does that for you.


        ----------------------------------------------------------
        The comparison seems to go like this: When you walk up to
        a unix box, it dares you to know how to get in, and once
        you do, it simply taunts you with a prompt. With Windows,
        the box suggests any number of things you might like to
        do, and then decides how it wants to get that done for
        you. It seems to me the Mac tells you what you want done,
        then does it for you and sends you on your way before you
        can do any damage. Turns out I'm a Unix guy. -- Jon Palmer
        ----------------------------------------------------------
      • Mike Flannigan
        ... You need to get familiar with the foreach statement/command. It s not the easiest command in Perl to learn, but it s not too bad. foreach $line(@array) {
        Message 3 of 8 , Dec 11, 2004
        • 0 Attachment
          > Date: Fri, 10 Dec 2004 14:46:44 -0800
          > From: "Jon Morris" <jon@...>
          > Subject: Writing to the middle of a file.
          >
          > How can I write a line of text to the middle of a file? From what I understand I need to push the file to an array, modify the array and then write back to a new file?
          >
          > If this is the easiest way of accomplishing this, how would I run through an array until I find a match, insert my string after that match, and push all the other data down one index?
          >
          > Thanks,
          > Jon

          You need to get familiar with the "foreach" statement/command.
          It's not the easiest command in Perl to learn, but it's not too
          bad.

          foreach $line(@array) {

          or just

          foreach (@array) {


          What you want to do is easy, but we are not going to just
          do it for you.


          Mike
        • Jonathan Paton
          ... Worth mentioning there is no difference between C and C , except that in Perl 6 there won t be a C . Also, the first example is best
          Message 4 of 8 , Dec 11, 2004
          • 0 Attachment
            > You need to get familiar with the "foreach" statement/command.
            > It's not the easiest command in Perl to learn, but it's not too
            > bad.
            >
            > foreach $line(@array) {
            >
            > or just
            >
            > foreach (@array) {

            Worth mentioning there is no difference between C<for> and C<foreach>,
            except that in Perl 6 there won't be a C<foreach>.

            Also, the first example is best shown as:

            for my $line (@array) {

            due to that variable declaration. I love warnings and strict.

            Jonathan Paton
          • merlyn@stonehenge.com
            ... Jonathan Worth mentioning there is no difference between C and Jonathan C , except that in Perl 6 there won t be a Jonathan C . Uh,
            Message 5 of 8 , Dec 11, 2004
            • 0 Attachment
              >>>>> "Jonathan" == Jonathan Paton <jepaton@...> writes:

              Jonathan> Worth mentioning there is no difference between C<for> and
              Jonathan> C<foreach>, except that in Perl 6 there won't be a
              Jonathan> C<foreach>.

              Uh, wait. There's no difference in Perl5 between the keywords "for"
              and "foreach", but there's most definitely a "for" loop (C-style)
              and a "foreach" loop (csh-style).

              And Perl6 will have both kinds of loops as well, except the foreach
              loop will now be spelled "for", and the for loop will be spelled
              "loop".

              So, your statement is wrong on many levels. :) Please consider
              that before answering further questions.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
              See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
            • Jonathan Paton
              Dear Randal (and list), ... Technically, you can do: foreach (my $i = 1; $i
              Message 6 of 8 , Dec 11, 2004
              • 0 Attachment
                Dear Randal (and list),

                > Uh, wait. There's no difference in Perl5 between the keywords "for"
                > and "foreach", but there's most definitely a "for" loop (C-style)
                > and a "foreach" loop (csh-style).

                Technically, you can do:

                foreach (my $i = 1; $i < 100; $i++) {
                print $i . "\n";
                }

                Although that is just being evil. I do understand the difference between
                the two loop styles, I just should have used clearer wording... like you
                did!

                > And Perl6 will have both kinds of loops as well, except the foreach
                > loop will now be spelled "for", and the for loop will be spelled
                > "loop".

                Agreed. Good idea too.

                > So, your statement is wrong on many levels. :) Please consider
                > that before answering further questions.

                Jonathan Paton
              • merlyn@stonehenge.com
                ... Jonathan Dear Randal (and list), ... Jonathan Technically, you can do: Jonathan foreach (my $i = 1; $i print $i . n ;
                Message 7 of 8 , Dec 11, 2004
                • 0 Attachment
                  >>>>> "Jonathan" == Jonathan Paton <jepaton@...> writes:

                  Jonathan> Dear Randal (and list),
                  >> Uh, wait. There's no difference in Perl5 between the keywords "for"
                  >> and "foreach", but there's most definitely a "for" loop (C-style)
                  >> and a "foreach" loop (csh-style).

                  Jonathan> Technically, you can do:

                  Jonathan> foreach (my $i = 1; $i < 100; $i++) {
                  Jonathan> print $i . "\n";
                  Jonathan> }

                  Uhm, yes, I said that. What part of what I said would have said
                  otherwise?

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                  See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
                • Jon Morris
                  ... Thanks, This gave me the kick in the rear that I needed. I am actually using foreach elsewhere in the same script. since the file is only about 400
                  Message 8 of 8 , Dec 13, 2004
                  • 0 Attachment
                    >You need to get familiar with the "foreach" statement/command.
                    >It's not the easiest command in Perl to learn, but it's not too
                    >bad.
                    >
                    >foreach $line(@array) {
                    >
                    >or just
                    >
                    >foreach (@array) {
                    >
                    >
                    >What you want to do is easy, but we are not going to just
                    >do it for you.
                    >
                    >
                    >Mike

                    Thanks,
                    This gave me the kick in the rear that I needed. I am actually using foreach elsewhere in the same script. since the file is only about 400 lines, this will work just fine.

                    Thanks,

                    Jon
                  Your message has been successfully submitted and would be delivered to recipients shortly.