Help (word selection)
am 02.09.2006 16:41:01 von Truty
Hi,
Please help me,
I would like to realise a filtre to select only word in file with
caracteres include into this variable $carac_available;
but not caracteres include into this variable $carac_notavailable;
my name file is "dico.txt"
sample with $carac_available = 'eo'; and $carac_notavailable =
'hydngp';
word selected : close, mouve, ... because 'o' and 'e' is include into
this words and 'h', 'y', 'd', 'n', 'g', 'p' is not include into this
words.
content dico.txt :
doing
close
sunny
drugs
mouve
....
my code with problem :
open (FILE_ANSWER,"dico.txt.txt");
while ($ligne = ) {
chomp($ligne);
if (($ligne =~ \[$carac_available]\) && ($ligne !~
\[$carac_notavailable]\) ) { print $ligne." | "; }
}
close FILE_ANSWER;
Please, can you please me ?
Re: Help (word selection)
am 02.09.2006 19:58:25 von someone
Truty wrote:
>
> Please help me,
> I would like to realise a filtre to select only word in file with
> caracteres include into this variable $carac_available;
> but not caracteres include into this variable $carac_notavailable;
>
> my name file is "dico.txt"
>
> sample with $carac_available = 'eo'; and $carac_notavailable = 'hydngp';
> word selected : close, mouve, ... because 'o' and 'e' is include into
> this words and 'h', 'y', 'd', 'n', 'g', 'p' is not include into this words.
>
> content dico.txt :
> doing
> close
> sunny
> drugs
> mouve
> ...
Your code should start with:
use warnings;
use strict;
to let perl help you catch any mistakes.
> my code with problem :
> open (FILE_ANSWER,"dico.txt.txt");
You should *ALWAYS* verify that the file opened correctly:
open FILE_ANSWER, '<', 'dico.txt.txt' or die "Cannot open 'dico.txt.txt' $!";
> while ($ligne = ) {
> chomp($ligne);
> if (($ligne =~ \[$carac_available]\) && ($ligne !~
\[] says to return a reference to an anonymous array. You want to use /
instead of \ for the match operator.
> \[$carac_notavailable]\) ) { print $ligne." | "; }
> }
> close FILE_ANSWER;
It looks like you want to ignore words with $carac_notavailable so something
like this should work:
while ( my $ligne = ) {
chomp $ligne;
next if $ligne =~ /[$carac_notavailable]/;
print "$ligne | " if $ligne =~ /[$carac_available]/;
}
John
--
use Perl;
program
fulfillment