473,403 Members | 2,293 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,403 software developers and data experts.

How can i CHANGE one specific word in a line????

16
hello,

im having a problem to change a particular word in a line, can´t find where is my mistake

also i would like to know how to make this program check automatically if a particular word exists on the line you got to you exchange it or not

i didnt find the right condition to do it



Expand|Select|Wrap|Line Numbers
  1.  
  2. #!C:/perl/bin/perl.exe  #Handle Area - in - outfile     
  3.  
  4. start:    open my $file, q{c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci} or die "Can't open input file"; 
  5.  
  6. open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";         
  7.  
  8. my @read = <$file>; 
  9. print "This file has: " .scalar(@read) . " lines\n";   
  10.  
  11. print "which line you want to get?\n"; 
  12.  
  13.             chomp (my $match = <STDIN>);          
  14.  
  15.             print $read[$match];         
  16.  
  17.         foreach($match) { print "$_\n"; }   
  18.  
  19.             print "exchanging word\n";    
  20.  
  21.             print "which word need exchange\?\n";    
  22.  
  23.         $let=<STDIN>; chomp $let;   
  24.  
  25.             print "change $let for...\n";   
  26.  
  27.         $new=<STDIN>; chomp $new;    
  28.  
  29.         if($new eq $let) { print "the word cannot be matched \n"; sleep 2; goto start;}    
  30.  
  31.         else { print "starting exchange...\n\n";   
  32.  
  33.         foreach ($match) {    s/$let/$new/g;    print "$_\n";    } 
  34.  
  35.             print "\n backing to..\nto exit ctrl+c\n\n"; sleep 5; goto start;    }  
  36.  
  37.  
Thank You
Jun 1 '09 #1
3 3156
Hi Sevla,

Your code formatting could do with improvement ;) Below is one way to tackle this
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3. open (my $FILE, "<", "test_file");
  4. my @contents=<$FILE>;
  5. my $line_count=scalar(@contents);
  6. print "This file has $line_count lines.\n";
  7. print "which line do you want to check [Enter a number between 1 and $line_count]";
  8. chomp(my $line=<STDIN>);
  9. print "The line is: $contents[$line -1]";
  10. my @words=split (/\s+/,$contents[$line -1]);
  11. for (my $index=0;$index <@words;$index++){
  12.    print "\t$index . $words[$index]\n";
  13. }
  14. print "Please enter the number of the word you wish to change:";
  15. chomp(my $old=<STDIN>);
  16. print "Change $words[$old] to ...";
  17. chomp(my $new=<STDIN>);
  18. $contents[$line -1 ] =~s/$words[$old]/$new/;
  19. print $contents[$line -1];
  20.  
Jun 19 '09 #2
numberwhun
3,509 Expert Mod 2GB
Please add the following lines after the first line, re-run the code and take care of any extra errors that are produced:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
All code you produce should contain these two lines after the shebang line.

Regards,

Jeff
Jun 20 '09 #3
KevinADC
4,059 Expert 2GB
Sevla posted this question on a few forums so don't be surprised if Sevla never comes back here to read any replies.
Jun 20 '09 #4

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

Similar topics

4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
2
by: Larry C | last post by:
Hello, I have an XML doc and I am trying to write a utility thath will allow me to change a particular word with my "title" section of theXML doc. I would like to look for a "word" in the title...
5
by: akelly_image | last post by:
Okay, if anyone could toss me some idea's here, please bare with my noobish questions, I just picked up VB2005 Pro about a week ago. ( no prior VB at all ) Here's my issue.. I'm pulling...
0
by: Twinkle | last post by:
HI Guys i am developing a Tool which will be compared with two different files but files data should be same.i am using that below code it's working and displaying the error into textbox. but i...
5
by: raha | last post by:
hi. Is there any body to help me? I am writing a web program. I have some forms. each form have some controls and users can edit the controls. finally they can save their forms. I would like...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
20
by: Joel Teichroeb | last post by:
In trunk of the svn there is a folder called PCbuild. Now lets say that I am running linux on my Personal Computer and want to build python. I go into the PCbuild directory, but wait. This is for...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.