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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sevla
    New Member
    • May 2009
    • 16

    #1

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

    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



    Code:
    #!C:/perl/bin/perl.exe  #Handle Area - in - outfile     
    
    start:    open my $file, q{c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci} or die "Can't open input file"; 
    
    open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";         
    
    my @read = <$file>; 
    print "This file has: " .scalar(@read) . " lines\n";   
    
    print "which line you want to get?\n"; 
    
    			chomp (my $match = <STDIN>);          
    
    			print $read[$match];         
    
    		foreach($match) { print "$_\n"; }   
    
    			print "exchanging word\n";    
    
    			print "which word need exchange\?\n";    
    
    		$let=<STDIN>; chomp $let;   
    
    			print "change $let for...\n";   
    
    		$new=<STDIN>; chomp $new;    
    
    		if($new eq $let) { print "the word cannot be matched \n"; sleep 2; goto start;}    
    
    		else { print "starting exchange...\n\n";   
    
    		foreach ($match) {    s/$let/$new/g;    print "$_\n";    } 
    
    			print "\n backing to..\nto exit ctrl+c\n\n"; sleep 5; goto start;    }
    Thank You
  • Skrynesaver
    New Member
    • Apr 2008
    • 4

    #2
    Hi Sevla,

    Your code formatting could do with improvement ;) Below is one way to tackle this
    Code:
    #! /usr/bin/perl
    
    open (my $FILE, "<", "test_file");
    my @contents=<$FILE>;
    my $line_count=scalar(@contents);
    print "This file has $line_count lines.\n";
    print "which line do you want to check [Enter a number between 1 and $line_count]";
    chomp(my $line=<STDIN>);
    print "The line is: $contents[$line -1]";
    my @words=split (/\s+/,$contents[$line -1]);
    for (my $index=0;$index <@words;$index++){
       print "\t$index . $words[$index]\n";
    }
    print "Please enter the number of the word you wish to change:";
    chomp(my $old=<STDIN>);
    print "Change $words[$old] to ...";
    chomp(my $new=<STDIN>);
    $contents[$line -1 ] =~s/$words[$old]/$new/;
    print $contents[$line -1];

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Please add the following lines after the first line, re-run the code and take care of any extra errors that are produced:

      Code:
      use strict;
      use warnings;
      All code you produce should contain these two lines after the shebang line.

      Regards,

      Jeff

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

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

        Comment

        Working...