Click to See Complete Forum and Search --> : How can i CHANGE one specific word in a line?!?!


Sevla
06-01-2009, 04:02 PM
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

here it is , the code:

Thank You



#!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 ; goto start; }

Sixtease
06-02-2009, 03:37 AM
The foreach($match) bit looks bogus. I assume the user is supposed to enter a line number, which is saved to $match. Then
foreach ($match) { s/$let/$new/g; } changes $let into $new inside the numeric string that the user entered. I'd suggest a lot of changes into your code but with minimum change to start making sense, this is what I'd do:
#!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 $line_no = <STDIN>);
my $match = read[$line_no];

print $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 ; goto start; }
So now $match is the line that the user chose, not its number.

Sevla
06-02-2009, 08:32 AM
Thanks a lot, for help !!!

Sevla
06-02-2009, 08:57 AM
by the way, which declar, i write to make this program see if the line i typed has the particular word before start asking for exchanges??

for instance, i need to make this program see if in "X" line has the word "Z", if its, it asks for exchange, if its not, it backs to the start!!




Thanks for your kind patience

Sixtease
06-02-2009, 10:23 AM
How about this:
#!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 $line_no = <STDIN>);
my $match = read[$line_no];

print $match, "\n";
#instead of: foreach($match) { print "$_\n"; }

print "exchanging word\n";

print "which word need exchange?\n";

my $let = <STDIN>; chomp $let;

print "change $let for...\n";

my $new = <STDIN>; chomp $new;

if ($new eq $let) {
print "the word cannot be matched \n";
sleep 2;
}

##### HERE IS THE IMPORTANT ADDITION
elsif ($match !~ /$let/) {
print "The given string is not contained in the line";
}
##### END OF THE ADDITION

else {
print "starting exchange...\n\n";

$match =~ s/$let/$new/g;
print "$match\n";

print "\n backing to..\nto exit ctrl+c\n\n";
sleep;
}

goto start; # we do it in every branch, so we can take it out of the if statement

In addition to checking the input string for presence in the line, as you wanted, I made some cosmetic modifications to the code, like indentation, removal of the foreach($match) construct and putting "goto start" after the if statement.

Still, there are way more serious issues you should address:

You use the string from the input to construct regexp. If the user enters e.g. "O.K.", you'll match "OAKS" as well, because dot is a regexp metachar. This can be addressed by escaping the metacharacters
(my $let_regex = $let) =~ s/([^\w\s])/\\$1/g;
goto start re-opens the files without closing them.
use strict and warnings.
You have filenames hard-wired in your source code -- at least put them into constants at the top of the file.

I haven't tested this, so it may not run.

Sevla
06-02-2009, 11:13 AM
i got it THANKS

ill just boring you with a little last question about this prog.

if i want to define the word e.g "DS" as default on the search, that every line i type in, the program see if "DS" its on this line and asks for exchange, or it warns DS does not exists at this line and goto start,

i can do this just by changing the $let for $let_regex ???

i made this but the program still allow me making exchanges even in line that does not have "DS" on it

i just want to change the lines that has this particular word, otherwise, gives just a warn

Sixtease
06-02-2009, 12:05 PM
So if I get it right, then, assuming these values:
$match: 'This is a test line',
$let: 'test',
$new: 'processed'
You would like to produce a warning and leave the line alone, while with these values:
$match: 'This is a test DS line',
$let: 'test',
$new: 'processed'
You would like to output
This is a processed DS line
right?

Sevla
06-02-2009, 12:44 PM
i think thats the most right logical reasoning,

i need this program to be a checking for a word, in this case "DS", and i need this guy to check if the line typed on STDIN has this "DS" and exchanges it for any other word,

theres no need to type on the command which word i need to exchange if my word for check is DS by default, so im having problems in how to make this guy check for this word before do anything else!

Sixtease
06-02-2009, 01:27 PM
Sorry, I'm getting lost. Could you provide examples of inputs and outputs like I did above to show what exactly it should do? Because what you describe here doesn't seem to match with what I wrote in the example.

Sevla
06-02-2009, 02:16 PM
im not so good to explain my programs yet, but ill try:

ill try explain like this:


start:

$file = text.txt
$check = word ''X''
$line = <STDIN> ### here the $line may be any line i search for
$new = <STDIN>### here the $new may be any word instead of 'X'

if (the $check exists on $line) print, "change $check for ...\n";

exchange $check for $new;

print " the text with the "X" replaced by any word else"


else "the word "X" was not found, or any text else" goto start;



that is the point i want to get, sry if could not explains much clearer, my english is bad

Thanks for your patience

Sevla
06-02-2009, 03:50 PM
did you get what a mean???

Sevla
06-02-2009, 05:18 PM
i got it man, i think you will get what i was talking about when see the code:

PS: i just can´t output result to a external new file, i just can print on the command, but not save the print on a new txt file.

if you get wheres my mistake on the code please tell me




#!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";

$check = 'DS';


print "which line you want to get?\n";


chomp (my $line_no = <STDIN>);

my $match = @read[$line_no];

print $match, "\n";

if ($match !~ /$check/) {

print "DS not found \n"; goto start;

} else {
print "change $check for...\n";

my $new = <STDIN>; chomp $new;


$match =~ s/$let/$new/g;

print "$match\n";

goto start;
}

Sixtease
06-03-2009, 02:39 AM
Yes, just replace print $something; for print OUTFILE $something where you want the output to go to the file. For example, instead of print $match, "\n"; say print OUTFILE $match, "\n";
But you should really stop re-opening the files without having closed them every time you call "goto start".

Sevla
06-03-2009, 08:16 AM
ok, thanks a lot

Sevla
06-03-2009, 08:35 AM
Does this forum talk only about technical topics or discuss about perl as a whole?

as you may have seen, im new on programming such as in perl either, so if there is a page to talk not only about technical stuffs, but whole perl staff!!

Sixtease
06-03-2009, 09:50 AM
Perl stuff... not technical... well, perlmonks' poetry section comes to mind: http://perlmonks.org/?node=Perl Poetry