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 . -
#!\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
12 1503
Hi kevin,
Still i am getting the same error message. I have include 'my' in the program. -
#!\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;
-
foreach my $file (@file){
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
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.
Either edit the file inplace or edit the array (@file) and overwrite the existing file (IN) with the contents of @array.
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?
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: -
#!\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;
-
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!
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..... ;)
HI Ramesh,
Have a look at the following sample code and you can replace what I have placed for you.... Snippet#1 -
$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 -
$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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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=...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |