words selection

D

David Squire

Truty said:
David Squire avait écrit le 02/09/2006 :

but with this line I haven't all words result, because
$carac_available is empty and $carac_notavailable isn't empty
or
$carac_available isn't empty and $carac_notavailable is empty

Well, I can't guess your requirements, you know :) Are both situations
relevant? What do you want to do in each case, i.e. under what
conditions is a word considered valid if one of these strings is empty?


DS
 
T

Truty

David Squire a présenté l'énoncé suivant :
Well, I can't guess your requirements, you know :) Are both situations
relevant? What do you want to do in each case, i.e. under what conditions is
a word considered valid if one of these strings is empty?


DS

a word is valid if this word contain all caracteres in $carac_available
and no caracteres of $carac_notavailable
but at the begining, the code don't working because $carac_available or
$carac_notavailable could be empty.
 
D

David Squire

Truty said:
David Squire a présenté l'énoncé suivant :

a word is valid if this word contain all caracteres in $carac_available
and no caracteres of $carac_notavailable

Yes, that was already clear. The question is, what do you want to do in
the special cases where the strings are empty? If $carac_available is
empty, does that mean that any word is OK so long as it does not contain
any characters from $carac_notavailable?

I think I can guess the behaviour you want. If I am right, this will do it:

----

#!/usr/bin/perl

use strict;
use warnings;

my $carac_available = 'eo';
my $carac_notavailable = 'hydngp';

while (my $ligne = <DATA>) {
chomp $ligne;
my $contains_all_carac_available = 1;
if ($carac_available) { # if $carac_available is empty, then there
# are no compulsory characters
$contains_all_carac_available &= ($ligne =~ /$_/) for split //,
$carac_available;
}
if (
($contains_all_carac_available
&& (!$carac_notavailable # if $carac_notavailable is empty,
# then all characters are OK
|| $ligne !~ /[$carac_notavailable]/
)
)
) {
print $ligne." | ";
}
}
 
M

Mumia W.

Ok, this [David Squire's] reply interest me but if __DATA __ content this :
doing
close
sunny
drugs
mouve
botts

my output will be this :
close | mouve |

and not :
close | mouve | botts |

because botts haven't 'e' caracteres.
[...]

I didn't use the DATA section, but you'll get the idea:


use strict;
use warnings;
my $carac_available = 'eo';
my $carac_notavailable = 'hydngp';
my $dico_txt = q{
doing
close
sunny
drugs
mouve
botts
};

my ($ca, $cn) = ($carac_available, $carac_notavailable);
open (FH, '<', \$dico_txt) or die("Oops:$!\n");

while (my $line = <FH>) {
next unless $line =~ m/(\w+)/;
my ($word) = ($1);

if (length($ca) == scalar(map $word =~ m/$_/, split //,
$ca)) {
unless ($word =~ m/[$cn]/) {
print "$word\n";
}
}

}

close (FH);
 
K

Klaus

Truty said:
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 = <FILE_ANSWER>) {
chomp($ligne);
if (($ligne =~ \[$carac_available]\) && ($ligne !~
\[$carac_notavailable]\) ) { print $ligne." | "; }
}
close FILE_ANSWER;

Here is a program:
====================================
use strict;
use warnings;

my $carac_available = 'eo';
my $carac_notavailable = 'hydngp';

open my $FILE_ANSWER, '<', 'dico.txt' or die "Error <dico.txt ($!)";

while (my $ligne = <$FILE_ANSWER>) {
chomp($ligne);
if ($ligne =~ /[\Q$carac_available\E]/
and $ligne !~ /[\Q$carac_notavailable\E]/) {
print $ligne, ' | ';
}
}

close $FILE_ANSWER;
====================================

here is the content of 'dico.txt':
====================================
doing
close
sunny
drugs
mouve
====================================


and here is the ouput:
====================================
close | mouve |
====================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top