473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 .
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
12 1503
KevinADC
4,059 Expert 2GB
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
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
4,059 Expert 2GB
foreach my $file (@file){
Jul 14 '08 #4
numberwhun
3,509 Expert Mod 2GB
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
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
4,059 Expert 2GB
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
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
410 Expert 256MB
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
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB

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

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

Similar topics

6
by: andrea.gavana | last post by:
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
15
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: WT | last post by:
Hello, I was using hastable to store collection of objects of the same class MyClass. I start replacing this hastable by a Dictionary<string,MyClass>....but I was storing this hastable in cache...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
0
by: Paul Hobbs | last post by:
Hi, I wanted to create a function which forced a user to enter no more than a specified number of characters. The code for this is below. The idea was simple, if a user is only meant to enter 10...
4
by: Nate12o6 | last post by:
Mabee you guys can help me with this. I have a form with enctype="multipart/form-data" that has a textarea in it as well as file upload. The text area is for a description of the file. If...
2
by: gsuns82 | last post by:
Hi all, I have to replace accented characters from a input string with normal plain text.I have coded as follows. String input = "ÄÀÁÂÃ"; input=...
18
by: J.K. Baltzersen | last post by:
To whomever it may concern: I am using MS Visual C++ 6.0. I have a process A which instantiates an object C. At a later point the process A creates the thread B. The thread B has access...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.