By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
3 Replies


P: 4
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
Expert Mod 2.5K+
P: 3,503
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
Expert 2.5K+
P: 4,059
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

Post your reply

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