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

Re: Re : [PBML] Problem

Expand Messages
  • Louis-Philippe
    ... ============Forwarded Mail============ From : l0u1sph1l1ppe@spiraliens.org To : merlyn@stonehenge.com Date :Thu, 17 Jul 2008 14:10:33 -0400 Subject : Re:
    Message 1 of 9 , Jul 17, 2008
    • 0 Attachment
      ----l0u1sph1l1ppe@... wrote ----

      ============Forwarded Mail============
      From : l0u1sph1l1ppe@...
      To : merlyn@...
      Date :Thu, 17 Jul 2008 14:10:33 -0400
      Subject : Re: Re : [PBML] Problem
      ============Forwarded Mail============

      Hi!

      randal> "The proper way to use in-place editing is to learn
      what $^I does."

      I thought about that Randal...
      but I believe the initial question from Ramesh involved appending to the file as opposed to in-place editing.
      In my experience, I couldn't think of a way to use $^I in this problem. I'd be happy if you prove me wrong! ;)

      here is what Ramesh posted:

      __________________________________________________________________

      Hi LP ,

      Thanks for your advice.

      I have a small problem with my code.

      I have a text file z.txt with following lines

      *transformation
      X Y Z
      10 20 30

      Finally i need to have a the same text file as follows,

      *transformation
      X Y Z
      50 60 80

      But instead when I run the code, It again types the *transformation
      line without replacing the last line. The output i ghet from the code
      is as follows,

      *transformation
      X Y Z
      10 20 30

      *transformation

      My code is as follows,

      #!\usr\bin\perl
      use warnings;
      use strict;
      open (IN,'+<','z. txt');
      my @file = <IN>;
      my $count = 0;
      foreach my $file (@file){
      if ($file =~ /\*transformation/ ){
      $count = $count + 1;
      }
      if ( $count == 1 ){
      $count = $count + 1;
      }
      if ( $count == 2 ){
      $file =~ s/10 20 30/50 60 80/g;
      print IN $file;
      $count = 0;
      last;
      }
      }
      close IN;

      Could you please help me on this code.

      Regards,
      Ramesh


      ___________________________________________________________

      regards,

      L-P








      ----merlyn@... wrote ----

      >>>>> "Louis-Philippe" == Louis-Philippe <l0u1sph1l1ppe@...> writes:

      Louis-Philippe> Damn! I thought your alternative was elegant Sylvain!
      Louis-Philippe> ... seems like the wizard knows whats behind the map magic! ;)

      Well, here's the same thing written properly:

      Sylvain> map {$_=~ s(10 20 30)(50 60 80); print IN, $_;} @file;

      for (@file) {
      s/10 20 30/50 60 80/;
      print IN $_;
      }

      Oh, and that comma after "IN" is bad syntax, so you didn't try it,
      either.

      But the real problem is that your use of in-place editing will break if the
      file is more than 8K long. The proper way to use in-place editing is to learn
      what $^I does. We cover this in the llama book, so you can read about it
      there, or google for "in-place editing perl" online.

      Oh wait, I have a column on that:

      http://www.stonehenge.com/merlyn/UnixReview/col23.html

      There ya go. 10 years old, and still works.

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URLhttp://www.stonehenge.com/merlyn/>
      Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
      See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

      [Non-text portions of this message have been removed]
    • Louis-Philippe
      mmm, yes... thats what I thought at first sight, but look at his code, Ramesh is doing a lots of hoops counting iterations for someone who wants to change
      Message 2 of 9 , Jul 17, 2008
      • 0 Attachment
        mmm, yes...
        thats what I thought at first sight, but look at his code, Ramesh is doing a lots of hoops counting iterations for someone who wants to change in-place... so that is why I suggested something similar to the for loop you mentionned in your previous post. Maybe my interpretation is wrong, but Ramesh never answered my question about his programs intentions...

        L-P


        ----merlyn@... wrote ----

        >>>>> "Louis-Philippe" == Louis-Philippe <l0u1sph1l1ppe@...> writes:

        Louis-Philippe> I have a text file z.txt with following lines

        Louis-Philippe> *transformation
        Louis-Philippe> X Y Z
        Louis-Philippe> 10 20 30

        Louis-Philippe> Finally i need to have a the same text file as follows,

        Louis-Philippe> *transformation
        Louis-Philippe> X Y Z
        Louis-Philippe> 50 60 80

        This looks like an in-place update to me.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URLhttp://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

        [Non-text portions of this message have been removed]
      • Ramesh Govinda
        Hi Philippe, Sorry for the delayed reply. I would clear my problem, Actually i am writing a script to edit the input file from LS Dyna. The input file are
        Message 3 of 9 , Jul 17, 2008
        • 0 Attachment
          Hi Philippe,

          Sorry for the delayed reply. I would clear my problem,

          Actually i am writing a script to edit the input file from LS Dyna. The input file are always very huge.
          So my motive was to replace the coordinates in x, y , z direction which is 10, 20, 30 in my case to 50, 60 ,70.

          The input file has many lines with wording transformation. that was the reason, I search for *Transformation which occurs only once in the file and then I skip the following 2 lines and then i gotot the coordinates lines where i replace the present coordinates with new value.

          Now i have a new query, instead of directly replacing  the values, i want to replace the line mathematically

          eg: I need to write in my script as follows

           input line   10+40 20+40 30+40

          Output line   50    60       70

          I want to write in the script that I will be adding 40 to the coordinates and the new line should be displayed as 50 60 70.

          I had written one code with while loop,

          open (IN,'+<','./z.txt');
          open OUT,"+<./z.txt";
          while (<IN>){
          my $file = $_;
          if ($file =~ /\*DEFINE_TRANSFORMATION/){
          print OUT $file; #print the line
          my $dump=<IN>; # skip to next line
          print OUT $dump;
          $file=<IN>; # skip to the required line

          $file =~ s/10        20        30/50        60        70/g;
          print OUT $file; 
          }
          close IN;
          close OUT;




          I have one more query to add to the above. When i run the script, the file is opened and then edited and then saved again. Is there any option in Perl, where in I could edit the text using the script without opening the file, something like a streaming function. This could make the script run faster..

          Regards,
          Ramesh
           


          --- On Thu, 7/17/08, Louis-Philippe <l0u1sph1l1ppe@...> wrote:
          From: Louis-Philippe <l0u1sph1l1ppe@...>
          Subject: Re: Re : [PBML] Problem
          To: merlyn@...
          Cc: perl-beginner@yahoogroups.com
          Date: Thursday, July 17, 2008, 6:30 PM











          mmm, yes...

          thats what I thought at first sight, but look at his code, Ramesh is doing a lots of hoops counting iterations for someone who wants to change in-place... so that is why I suggested something similar to the for loop you mentionned in your previous post. Maybe my interpretation is wrong, but Ramesh never answered my question about his programs intentions.. .



          L-P



          ----merlyn@stonehen ge.com wrote ----



          >>>>> "Louis-Philippe" == Louis-Philippe <l0u1sph1l1ppe@ spiraliens. org> writes:



          Louis-Philippe> I have a text file z.txt with following lines



          Louis-Philippe> *transformation

          Louis-Philippe> X Y Z

          Louis-Philippe> 10 20 30



          Louis-Philippe> Finally i need to have a the same text file as follows,



          Louis-Philippe> *transformation

          Louis-Philippe> X Y Z

          Louis-Philippe> 50 60 80



          This looks like an in-place update to me.



          --

          Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

          <merlyn@stonehenge. com> <URLhttp://www. stonehenge. com/merlyn/>

          Smalltalk/Perl/ Unix consulting, Technical writing, Comedy, etc. etc.

          See http://methodsandme ssages.vox. com/ for Smalltalk and Seaside discussion



          [Non-text portions of this message have been removed]





























          [Non-text portions of this message have been removed]
        • merlyn@stonehenge.com
          ... Ramesh Now i have a new query, instead of directly replacing  the values, i Ramesh want to replace the line mathematically Ramesh eg: I need to write
          Message 4 of 9 , Jul 17, 2008
          • 0 Attachment
            >>>>> "Ramesh" == Ramesh Govinda <ramesh.govinda@...> writes:

            Ramesh> Now i have a new query, instead of directly replacing  the values, i
            Ramesh> want to replace the line mathematically

            Ramesh> eg: I need to write in my script as follows

            Ramesh>  input line   10+40 20+40 30+40

            Ramesh> Output line   50    60       70

            Ramesh> I want to write in the script that I will be adding 40 to the
            Ramesh> coordinates and the new line should be displayed as 50 60 70.

            You need to learn about in-place editing. Opening the
            same name for input and output, even with +<, will break for you.

            Please read the column and/or documentation I referenced previously.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
            See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
          • Louis-Philippe
            Hi Ramesh, as Randal says, better use the inplace editing, you ll have fun with it! to give you a starter, I would code it like this: $^I = ~ ; push @ARGV,
            Message 5 of 9 , Jul 18, 2008
            • 0 Attachment
              Hi Ramesh,

              as Randal says, better use the inplace editing, you'll have fun with it!

              to give you a starter, I would code it like this:

              $^I = '~';
              push @ARGV, 'z.txt';

              while (<>) {
              s/(\d+)/40 + $1/ge;
              print;
              }

              look at Randal's column and you'll understand the code ;)

              L-P



              ----ramesh.govinda@... wrote ----

              Hi Philippe,

              Sorry for the delayed reply. I would clear my problem,

              Actually i am writing a script to edit the input file from LS Dyna. The input file are always very huge.
              So my motive was to replace the coordinates in x, y , z direction which is 10, 20, 30 in my case to 50, 60 ,70.

              The input file has many lines with wording transformation. that was the reason, I search for *Transformation which occurs only once in the file and then I skip the following 2 lines and then i gotot the coordinates lines where i replace the present coordinates with new value.

              Now i have a new query, instead of directly replacing the values, i want to replace the line mathematically

              eg: I need to write in my script as follows

              input line 10+40 20+40 30+40

              Output line 50 60 70

              I want to write in the script that I will be adding 40 to the coordinates and the new line should be displayed as 50 60 70.

              I had written one code with while loop,

              open (IN,'+<','./z.txt');
              open OUT,"+<./z.txt";
              while (<IN>){
              my $file = $_;
              if ($file =~ /\*DEFINE_TRANSFORMATION/){
              print OUT $file; #print the line
              my $dump=<IN>; # skip to next line
              print OUT $dump;
              $file=<IN>; # skip to the required line
              }
              $file =~ s/10 20 30/50 60 70/g;
              print OUT $file;
              }
              close IN;
              close OUT;

              I have one more query to add to the above. When i run the script, the file is opened and then edited and then saved again. Is there any option in Perl, where in I could edit the text using the script without opening the file, something like a streaming function. This could make the script run faster..

              Regards,
              Ramesh


              --- On Thu, 7/17/08, Louis-Philippe <l0u1sph1l1ppe@...> wrote:
              From: Louis-Philippe <l0u1sph1l1ppe@...>
              Subject: Re: Re : [PBML] Problem
              To: merlyn@...
              Cc: perl-beginner@yahoogroups.com
              Date: Thursday, July 17, 2008, 6:30 PM

              mmm, yes...

              thats what I thought at first sight, but look at his code, Ramesh is doing a lots of hoops counting iterations for someone who wants to change in-place... so that is why I suggested something similar to the for loop you mentionned in your previous post. Maybe my interpretation is wrong, but Ramesh never answered my question about his programs intentions.. .

              L-P

              ----merlyn@stonehen ge.com wrote ----

              >>>>> "Louis-Philippe" == Louis-Philippe <l0u1sph1l1ppe@ spiraliens. org> writes:

              Louis-Philippe> I have a text file z.txt with following lines

              Louis-Philippe> *transformation

              Louis-Philippe> X Y Z

              Louis-Philippe> 10 20 30

              Louis-Philippe> Finally i need to have a the same text file as follows,

              Louis-Philippe> *transformation

              Louis-Philippe> X Y Z

              Louis-Philippe> 50 60 80

              This looks like an in-place update to me.

              --

              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

              <merlyn@stonehenge. com> <URLhttp://www. stonehenge. com/merlyn/>

              Smalltalk/Perl/ Unix consulting, Technical writing, Comedy, etc. etc.

              See http://methodsandme ssages.vox. com/ for Smalltalk and Seaside discussion

              [Non-text portions of this message have been removed]











              [Non-text portions of this message have been removed]




              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.