Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old June 1st, 2009, 07:22 PM
Newbie
 
Join Date: May 2009
Posts: 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
  #2  
Old June 19th, 2009, 01:39 PM
Newbie
 
Join Date: Apr 2008
Location: Ireland
Posts: 4

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


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.  
  #3  
Old June 20th, 2009, 08:29 PM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

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


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
  #4  
Old June 20th, 2009, 10:20 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

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


Sevla posted this question on a few forums so don't be surprised if Sevla never comes back here to read any replies.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete lines containing a specific word Francesco Pietra answers 2 January 7th, 2008 12:45 PM
MyISAM engine: worst case scenario in case of crash (mysql, O/S,hardware, whatever) alf answers 110 December 9th, 2006 05:25 PM
whether is the standard input stream full buffered or line buffered after calling function setbuf()? kernelxu@hotmail.com answers 9 November 15th, 2005 02:07 AM
Need help change a particular word in an XML doc Larry C answers 2 November 12th, 2005 03:57 AM