473,654 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

16 New Member
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 3170
Skrynesaver
4 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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
2609
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 QA tested for a new year release. I don't think that going to prove practical, but there is no harm in trying :) and some serious family commitments. But it has to be done soon so this is stage one. Mike Winter provided an extensive list of...
2
2864
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 and then change only that "word" to something else. Below in XML file I would want to change "Issue" to "Concern". This is one snip of the XML file there may be many more instances of the word "Issue" that I would want to change. I know how...
5
2754
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 information out of a text file and need to pull specific words out of a string. For example, the text file looks sorta like this:
0
1069
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 want highlight that missing word into Richtextbox2 which is displaying in textbox. Create two string s1,s2 in main class then private void button1_Click(object sender, System.EventArgs e) {
5
1867
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 to add an ability to my program. In fact I want to have an Icon that change, when a user change the value of a control of form such as textbox , radiobox, grid. as you know the most famous and important editors such as PSPAD or
7
3187
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 code I need to put it into. using System; using System.Drawing; using System.IO;
2
19742
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: print line f.close()
0
2077
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 446 27.436 34.037 56.145 1.00 0.00 H HETATM 7009 H2 WAT 446 27.049 33.827 54.563 1.00 0.00 H occurring in a 300MB file. In present case each three-lines block is followed by line renumbering (7007,
20
1904
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 windows not for any personal computer. Calling Windows PC seems to be something that Apple did so they would not have to directly mention Windows. Could all the places that say PC that are not referring to Personal Computers in general be...
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.