By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 2,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

Problem replacing the string

P: 37
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 .
Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl 
  2. use warnings;
  3. use strict;
  4. open IN, "z.txt";  
  5. @file = <IN>;
  6. $count = 0; 
  7. foreach $file (@file){ 
  8. if ($file =~ /\*transformation/){ 
  9. $count = $count + 1; 
  10. if ( $count == 1 ){ 
  11. $count = $count + 1; 
  12. if ( $count == 2 ){ 
  13. $file =~ s/ 10 20 30/50 60 80/g; 
  14. print IN $file; 
  15. $count = 0;
  16. last; 
  17. close IN;
  18.  
Looking forward to your response.

Ramesh
Jul 13 '08 #1
Share this Question
Share on Google+
12 Replies


KevinADC
Expert 2.5K+
P: 4,059
when you use the "strict" pragma you have to declare the private variables properly. You do that with "my" for the most part.

http://perldoc.perl.org/perlsub.html...ables-via-my()
Jul 13 '08 #2

P: 37
Hi kevin,

Still i am getting the same error message. I have include 'my' in the program.
Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl 
  2. use warnings;
  3. use strict;
  4. open IN, "z.txt";  
  5. my @file = <IN>;
  6. my $count = 0; 
  7. foreach $file (@file){ 
  8. if ($file =~ /\*transformation/){ 
  9. $count = $count + 1; 
  10. if ( $count == 1 ){ 
  11. $count = $count + 1; 
  12. if ( $count == 2 ){ 
  13. $file =~ s/ 10 20 30/50 60 80/g; 
  14. print IN $file; 
  15. $count = 0;
  16. last; 
  17. close IN;
  18.  
Jul 14 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
foreach my $file (@file){
Jul 14 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 14 '08 #5

P: 37
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.
Jul 14 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
Either edit the file inplace or edit the array (@file) and overwrite the existing file (IN) with the contents of @array.
Jul 14 '08 #7

P: 37
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?
Jul 15 '08 #8

nithinpes
Expert 100+
P: 410
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:
Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl 
  2. use warnings;
  3. use strict;
  4. open IN, "z.txt";  
  5. open OUT,">out.txt";
  6.  
  7. while (<IN>){ 
  8. my $file = $_;
  9. if ($file =~ /\*transformation/){ 
  10. print OUT $file; #print the line
  11. my $dump=<IN>; # skip to next line
  12. print OUT $dump;
  13. $file=<IN>; # skip to the required line
  14.  
  15. $file =~ s/10 20 30/50 60 80/g; 
  16. print OUT $file; 
  17.  
  18. close IN;
  19. close OUT;
  20.  
Jul 15 '08 #9

P: 37
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!
Jul 15 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
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..... ;)
Jul 15 '08 #11

P: 5
HI Ramesh,

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

Snippet#1
Expand|Select|Wrap|Line Numbers
  1. $string1 = "I love to eat apples all day. I can eat apples every day! Apples are yummy!";
  2. $string1 =~ s/apples/oranges/g;
  3. print "The resulting value is : $string1 \n"; 
  4.  
Snippet#2
Expand|Select|Wrap|Line Numbers
  1. $string1 = "today is the best day of all";
  2. if ($string1 =~ tr/thed/abc/){
  3. print "String1 now contains $string1. \n";
  4. } else {
  5. print "No match found.: \n";
  6. }  
  7.  
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
Jul 16 '08 #12

KevinADC
Expert 2.5K+
P: 4,059

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.
Jul 16 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.