Click to See Complete Forum and Search --> : Referring to an exact word in a line
Sevla
06-03-2009, 12:27 PM
i would like to know wich declar. narrows a reference by a single word in a line, idepending if there is a similar word in this line
e.g. fire / firecracker ## i just need the program print me only the line with "fire", not with firecracker or firestorm or firestone, and so on!!
perl_diver
06-03-2009, 05:03 PM
assuming "fire" is bound by a non-word character such as a space or punctuation, you can use the word-boundary anchor \b in a regexp to match a word and not a substring:
if ($foo =~ /\bfire\b/) {
...
}
that will match something like this:
The house is on fire.
but will not match something like this:
The firecracker is red.
Sevla
06-04-2009, 08:36 AM
i already set the condition if ..
if ($match !~ /$check/) {
###### Here the word "DS" is the key word on the reading line ######
print "DS not found in this line\n"; goto start;
i set the string $check with the 'DS' word
my $check = "DS";
and $match is the result from number of the line and it´s respective typed on <STDIN> from a file txt
how can i say to this program besides to check if there is the word 'DS' on a particular line but to narrow the check to do not mix 'DS' with e.g. "DSTRACT"
perl_diver
06-04-2009, 02:25 PM
What? I already showed and explained to you how to do that. Why have you not tried to implement my suggestion? You appear to have not even read what I posted, so I won't continue to waste my time trying to help you.
Sevla
06-04-2009, 02:41 PM
i wrote the reply becaus it didn´t work in my case, something wrong i have made, im trying to get the mistake point, that´s why i implemented your answer with some code of my program !!!! to you have an overview of what exactly i was talking about
and about wasting your time, you are free to help or do not help anyone as you want !!
perl_diver
06-04-2009, 03:00 PM
What did not work? There is no \b in the code you posted, you did not mention you tried using the \b anchor in the code and it did not work. Saying "it did not work" is useless, you have to describe in detail what you tried and what it did and post samples of the real data you are trying to parse.
perl_diver
06-04-2009, 03:03 PM
Here you have:
if ($match !~ /$check/) {
that is checking that the string does not have the value of $check (DS I assume) anywhere in the line. That is the exact opposite of what you are saying you want to do, which is to find DS in the string/line.
Sevla
06-04-2009, 03:30 PM
that condition i sent you was because i need this program to check any line i search for, and if the $check = DS do not exists in the line, shows a msg and go to start, if it do find, continues the code
your help was in condition too, so it could not fit 2 conditions for the same intention
above the whole code:
#!C:/perl/bin/perl.exe #Handle Area - in - outfile
use warnings;
use strict;
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";
### LINE SEARCH AREA ###
my $line_no = <STDIN>;
my $match = $read[$line_no];
print $match;
if ($match !~ /$check/) {
print "DS not found in this line\n"; goto start;
} else {
print "change $check for...\n";
my $new = <STDIN>; chomp $new;
$match =~ s/$check/$new/;
print OUTFILE $match, "\n";
}
perl_diver
06-04-2009, 03:37 PM
OK, so where is \b in your code? No where. Change this line:
$match =~ s/$check/$new/;
to:
$match =~ s/\b$check\b/$new/;
and see if that helps.
Sevla
06-04-2009, 04:17 PM
i replaced the "$match =~ s/\b$check\b/$new/;"
and my $check = "/\bDS\b/"; instead $check = "DS";
is that?
it still allows lines with "DSTRACT," and so on instead "DS"
perl_diver
06-04-2009, 05:00 PM
I give up. You are totally cluless and can't follow even the most simple instructions.
I am out of this thread.
Sevla
06-04-2009, 05:11 PM
OK, thanks for your non-help anyway
perl_diver
06-04-2009, 05:15 PM
I see you have this same series of questions on a number of forums and have met with similar results: nobody wanting to help you or trying to help and giving up because you're incapable of understaning anything.
You're not welcome.
Nedals
06-04-2009, 08:16 PM
You really do not use the help you are given. :(
Sixtease provided you a lot of help in your other thread that you disregarded.
perl diver gave you a lot of help and finally gave up
I will try once, and once only, unless you show some indication that you are reading what is posted.
Here is a sample of working code that attempts to solve your problem.
You will note that there is no GOTO (which you should hardly ever need to use) and that the file only gets opened once
#!C:/perl/bin/perl.exe #Handle Area - in - outfile
use warnings;
use strict;
# Open the INPUT file, read into an array, and close the file
#open INFILE, q{c:/perl/discoverEdit[1].4796.11.30.5.8.2009.dci} or die "Can't open input file";
my @read = <DATA>; #<INFILE>;
#close INFILE;
print "This file has: " .scalar(@read). " lines\n\n";
#open(OUTFILE, "> c:/perl/report.dci") or die "Can't open output file";
my $match = 'DS';
for (@read) {
chomp;
next unless $_;
if (/$match/) {
print "$match found. Change to:";
my $newtext = <STDIN>;
chomp $newtext;
s/$match/$newtext/g;
print "DS changed: : $_\n";
next;
}
print "DS not found: $_\n";
}
#close OUTFILE;
__DATA__
a line of text with DS
another line without it
yet another with DS in it
and so on
perl_diver
06-04-2009, 08:23 PM
Maybe you didn't read the whole thread Nedals, for which I don't blame you, but he only wants to change DS if it is not part of another string, which was why I recommended the \b anchor in the regexp. Your code will change it where ever it occurs in the lines/strings.
Nedals
06-04-2009, 08:36 PM
Maybe you didn't read the whole thread Nedals, Actually I did :),
I thought I would give the OP something to do and see if this gets read
perl_diver
06-04-2009, 10:17 PM
Actually I did :),
I thought I would give the OP something to do and see if this gets read
hehehe.... my kind of man :D
Sevla
06-05-2009, 01:50 PM
I apologize for any inconvenience on my part, im on very beginning programming such as perl and after read some books and tutorials during this few weeks i had some doubts, and i´ve searched for some forums, and solved most of my problems, untill this one !!
however, thanks Nedals and Diver !!
Richard.William
06-08-2009, 02:12 PM
i would like to know wich declar. narrows a reference by a single word in a line, idepending if there is a similar word in this line
e.g. fire / firecracker ## i just need the program print me only the line with "fire", not with firecracker or firestorm or firestone, and so on!!
To find the exact and only the word "fire" by itself in a file, use the following 3 liner. File is "X".
var str content ; cat "X" > $content
while ( { sen -r "^,file,^" $content } > 0 )
stex -r "^,file,^" $content
This is in biterscripting ( http://www.biterscripting.com ) . sen is the command that is String ENumerator (counter), stex is the command that is STring EXtractor, -r flag means treat the search string as regular expression, , (comma) means any one non-printable character. Use -c for case insensitive, ,file, is the search phrase (in regular expression) . Also, the search phrase ,file, can itself contain spaces. For example, it can be ,hello\, world, , which will search for hello, world . Feel free to translate this into your favorite scripting language.
Richard
perl_diver
06-08-2009, 04:15 PM
That bitterscripting dork found this forum too, he posts his bitterscripting spam on many forums. The forum may want to ban him now.