Problem replacing the string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramesh54
    New Member
    • Jul 2008
    • 37

    Problem replacing the string

    Hello All,

    I have a test file z.txt with following lines

    *transformation
    X Y Z
    10 20 30

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

    *transformation
    X Y Z
    50 60 80

    When i run the code in windows, I am getting the error
    Global symbol requires explicit package.

    Could you please let me know what is wrong .
    Code:
    #!\usr\bin\perl 
    use warnings;
    use strict;
    open IN, "z.txt";  
    @file = <IN>;
    $count = 0; 
    foreach $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;
    Looking forward to your response.

    Ramesh
    Last edited by numberwhun; Jul 13 '08, 11:03 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    when you use the "strict" pragma you have to declare the private variables properly. You do that with "my" for the most part.

    Comment

    • ramesh54
      New Member
      • Jul 2008
      • 37

      #3
      Hi kevin,

      Still i am getting the same error message. I have include 'my' in the program.
      Code:
      #!\usr\bin\perl 
      use warnings;
      use strict;
      open IN, "z.txt";  
      my @file = <IN>;
      my $count = 0; 
      foreach $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;
      Last edited by numberwhun; Jul 14 '08, 10:57 AM. Reason: Please use code tags

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        foreach my $file (@file){

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          I have now fixed your code tags twice and this is your warning. Code tags are not an option, they are required to surround any code that you place in a post in the forums.

          Use them please, or other actions will be taken.

          Regards,

          Moderator

          Comment

          • ramesh54
            New Member
            • Jul 2008
            • 37

            #6
            Now the program runs without error. But the output is different.

            It prints the output as follows

            *transformation
            x y z
            10 20 30
            *transformation

            But i wanted to replace 10 20 30 by 50 60 80 without editing the other lines. What could be the possible error.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Either edit the file inplace or edit the array (@file) and overwrite the existing file (IN) with the contents of @array.

              Comment

              • ramesh54
                New Member
                • Jul 2008
                • 37

                #8
                Hi Kevin,

                I would like to keep the input file as it is. What i am unable to understand is that , in my loop i skip the first 2 lines and then i replace the text in the 3 rd line and print only the third line. But the output I get is for the first line. What is wrong with my loop formation?

                Comment

                • nithinpes
                  Recognized Expert Contributor
                  • Dec 2007
                  • 410

                  #9
                  Originally posted by ramesh54
                  Hi Kevin,

                  I would like to keep the input file as it is. What i am unable to understand is that , in my loop i skip the first 2 lines and then i replace the text in the 3 rd line and print only the third line. But the output I get is for the first line. What is wrong with my loop formation?
                  Well, if you want to keep the input file as it is and to write modified output to a different file, you need to open another file for writing.
                  But then, you will not get the required output because of the logical error in your script.
                  You are not skipping to the third line in your script, but successively incrementing a variable $count and printing $file. The $file will still be referring to line containing *transformation .

                  You can try this:
                  Code:
                  #!\usr\bin\perl 
                  use warnings;
                  use strict;
                  open IN, "z.txt";  
                  open OUT,">out.txt";
                  
                  while (<IN>){ 
                  my $file = $_;
                  if ($file =~ /\*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 80/g; 
                  print OUT $file; 
                  
                  } 
                  close IN;
                  close OUT;

                  Comment

                  • ramesh54
                    New Member
                    • Jul 2008
                    • 37

                    #10
                    Hi Kevin,

                    Great! your code works well and I wanted to overwrite it in my input file itself and so i have corrected the mode to +< in output.
                    Thanks!

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by ramesh54
                      Hi Kevin,

                      Great! your code works well and I wanted to overwrite it in my input file itself and so i have corrected the mode to +< in output.
                      Thanks!

                      It was the easiest code I never wrote..... ;)

                      Comment

                      • alampally
                        New Member
                        • Jul 2008
                        • 5

                        #12
                        HI Ramesh,

                        Have a look at the following sample code and you can replace what I have placed for you....

                        Snippet#1
                        Code:
                        $string1 = "I love to eat apples all day. I can eat apples every day! Apples are yummy!";
                        $string1 =~ s/apples/oranges/g;
                        print "The resulting value is : $string1 \n";
                        Snippet#2
                        Code:
                        $string1 = "today is the best day of all";
                        if ($string1 =~ tr/thed/abc/){
                        print "String1 now contains $string1. \n";
                        } else {
                        print "No match found.: \n";
                        }
                        Either of the snippet will replace a string in your file.....

                        Good Luck,

                        Write me for any help in C, C++, Perl, PHP and UNIX related stuff. I will answer you with in the time with free of cost.

                        Rammohan Alampally,
                        HP Technologies
                        Bangalore

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          Originally posted by alampally

                          Either of the snippet will replace a string in your file.....
                          That is not accurate. The snippets will replace the search pattern with the replacement pattern in the string. He will need to use Tie::File or other method to update the lines of the file. Your code only processes a string, which is not the same as updating a line in a file.

                          Comment

                          Working...