From: David Squire on
Truty wrote:
> David Squire avait ?nonc? :
>> Truty wrote:
>>>
>>> CODE :
>>>
>>> #!/usr/bin/perl
>>> use strict;
>>> use warnings;
>>>
>>> # in @sayword is recording words already say
>>> # sample sayword : 'abcde' , 'kjuhy', '12345, 'KJITR' ...
>>> my $sayword[0]= 'abcde'; # for test
>>> # caracteres false is in this string
>>> my $carac_notavailable = 'zwxvbdsau'; # for test
>>> # caracteres OK is in this string
>>> my $carac_available = 'cea'; # for test
>>> my $line;
>>> my $fileanswer = length($sayword[0]); # to select the file '4
>>> caracteres' or '5 caracteres' or ......
>>> open (FILE,"$fileanswer.txt"); # file = 4.txt or 5.txt or ....
>>> while ($line = <FILE>) {
>>> chomp($line);
>>> if (($line =~ \[$carac_available]\) && ($ligne !~
>>> \[$carac_notavailable]\)) {
>>> print $line." | "; # display word available
>>> }
>>> }
>>> close FILE;
>>
>> ---- Attempting to run this produces -----
>>
>> Backslash found where operator expected at ./test2.pl line 17, near "]\"
>> (Missing operator before \?)
>> Backslash found where operator expected at ./test2.pl line 17, near "]\"
>> (Missing operator before \?)
>> syntax error at ./test2.pl line 7, near "$sayword["
>> syntax error at ./test2.pl line 17, near "]\"
>> Global symbol "$ligne" requires explicit package name at ./test2.pl
>> line 17.
>> syntax error at ./test2.pl line 20, near "}"
>> Execution of ./test2.pl aborted due to compilation errors.
>>
>> ----
>>
>> This code contains the same errors that I pointed out in your original
>> post, as well as some new ones. Please read my post and make the
>> necessary changes before posting again.
>>
>> You really should not post code that won't compile. The error messages
>> from the compiler and Perl's own documentation should get you past
>> syntax errors. I suggest that you read an introductory book on Perl,
>> and also become familiar with the documentation that comes with Perl.
>> For a start, try reading
>>
>> perldoc perldata
>> perldoc perlre
>>
>>
>> DS
>
> sorry :( but now there aren't error when you compile.
>
> A complable code :
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> # in @sayword is recording words already say
> # sample sayword : 'abcde' , 'kjuhy', '12345, 'KJITR' ...
> my @sayword;
> $sayword[0]= "abcde"; # for test
> # caracteres false is in this string
> my $carac_notavailable = "zwxvbdsau"; # for test
> # caracteres OK is in this string
> my $carac_available = "cea"; # for test

The variables $carac_notavailable and $carac_available are here just
strings, not compiled regular expressions as in the example I gave you...

> my $line;
> my $fileanswer = length($sayword[0]); # to select the file '4
> caracteres' or '5 caracteres' or ......
> open (FILE,"$fileanswer.txt"); # file = 4.txt or 5.txt or ....

You're still not checking that this is successful - you must. Nor are
you using a lexical file handle, or the three-argument form of open as
suggested.

Also, it is much better to use the __DATA__ technique (as in my example
code) for posts here, as then we can all test the scripts. We can't test
this, because we don't have your filesystem.

> while ($line = <FILE>) {
> chomp($line);
> if (($line =~ $carac_available) && ($line !~ $carac_notavailable)) {

Here you are using $carac_notavailable and $carac_available as if they
were compiled regular expressions, but they are not in this code.

> print $line." | "; # display word available
> }
> }
> close FILE;

Go back and look again at my first reply.


DS

From: Truty on
David Squire a expos? le 02/09/2006 :
> Truty wrote:
>> 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 :
>
> missing:
>
> use strict;
> use warnings;
>
>> open (FILE_ANSWER,"dico.txt.txt");
>
> Is this your real code? That filename does not match the one above, and you
> really, really must check that the open was successful. This would be much
> better:
>
> open my $FILE_ANSWER, '<', 'dico.txt' or die "Could not open dico.txt for
> reading: $!";
>
>> while ($ligne = <FILE_ANSWER>) {
>> chomp($ligne);
>> if (($ligne =~ \[$carac_available]\) && ($ligne !~
>> \[$carac_notavailable]\) ) { print $ligne." | "; }
>
> $carac_available and $carac_available are not defined in this example. Please
> post a *complete* script that we can run and test. Cut-and-paste the code,
> don't retype it.
>
> This will not even compile. You can't use backslashes as regex delimiters.
> The compiler should have told you that.
>
> You would probably be better off creating compiled regular expression
> variables rather than strings. See my example below.
>
>> }
>> close FILE_ANSWER;
>>
>
> ----
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $carac_available = qr([eo]);
> my $carac_notavailable = qr([hydngp]);
>
> while (my $ligne = <DATA>) {
> chomp $ligne;
> if (($ligne =~ $carac_available) && ($ligne !~ $carac_notavailable)) {
> print $ligne." | ";
> }
> }
>
> __DATA__
> doing
> close
> sunny
> drugs
> mouve
> botts
>
> ----
>
> Output:
>
> close | mouve | botts |
>
> ----
>
>
> DS

Ok, this 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.

if i define two string : (this string don't stay egal at 'eo' or
'hydngp', it is a variable)

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

could you tell me the correct code line to obtain the correct output ?

if (($ligne =~ $carac_available) && ($ligne !~ $carac_notavailable))
{ print $ligne." | "; }

please.


From: David Squire on
Truty wrote:
> David Squire a expos? le 02/09/2006 :
>>
>> ----
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $carac_available = qr([eo]);
>> my $carac_notavailable = qr([hydngp]);
>>
>> while (my $ligne = <DATA>) {
>> chomp $ligne;
>> if (($ligne =~ $carac_available) && ($ligne !~
>> $carac_notavailable)) {
>> print $ligne." | ";
>> }
>> }
>>
>> __DATA__
>> doing
>> close
>> sunny
>> drugs
>> mouve
>> botts
>>
>> ----
>>
>> Output:
>>
>> close | mouve | botts |
>>
>> ----
>>
>>
>> DS
>
> Ok, this reply interest me but if __DATA __ content this :
> doing
> close
> sunny
> drugs
> mouve
> botts

Of course that's what it contains. Try running the script. The __DATA__
section is part of it.

> my output will be this :
> close | mouve |
>
> and not :
> close | mouve | botts |
>
> because botts haven't 'e' caracteres.

You want the string to contain *all* the characters in
$carac_available? That is not what your example suggested at all. You
were trying to do something equivalent to

if (($ligne =~ /[eo]/) && ($ligne !~ /[hydngp]/)) {

This uses character classes. [eo] matches any character in 'eo'. It
doesn't require them all to be present. In words, that line means "If
$ligne contains any character in 'eo' and doesn't contain any character
in 'hydngp'..."

What you want is more difficult. Off the top of my head, I can't think
of a single regex that would do it (but I guess someone else will :) ).
How about this:

----

#!/usr/bin/perl

use strict;
use warnings;

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

while (my $ligne = <DATA>) {
chomp $ligne;
my $carac_available_copy = $carac_available;
while ($carac_available_copy && $ligne =~
/([$carac_available_copy])/) {
$carac_available_copy =~ s/$1//g;
}
if (!$carac_available_copy && ($ligne !~ /[$carac_notavailable]/)) {
print $ligne." | ";
}
}

__DATA__
doing
close
sunny
drugs
mouve
botts

----

Output:

close | mouve |

----

>
> if i define two string : (this string don't stay egal at 'eo' or
> 'hydngp', it is a variable)
>
> my $carac_available;
> my $carac_notavailable;
> ...
> $carac_available = 'eo';
> $carac_notavailable = 'hydngp';
> ...
>
> could you tell me the correct code line to obtain the correct output ?
>

if you want to use string variables, you can do so. I've already
addressed that in this thread. You just need to use valid regex
delimiters. Also see the examples above.


DS
From: David Squire on
David Squire wrote:

> What you want is more difficult. Off the top of my head, I can't think
> of a single regex that would do it (but I guess someone else will :) ).
> How about this:
>
> ----
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $carac_available = 'eo';
> my $carac_notavailable = 'hydngp';
>
> while (my $ligne = <DATA>) {
> chomp $ligne;
> my $carac_available_copy = $carac_available;
> while ($carac_available_copy && $ligne =~
> /([$carac_available_copy])/) {
> $carac_available_copy =~ s/$1//g;
> }
> if (!$carac_available_copy && ($ligne !~ /[$carac_notavailable]/)) {
> print $ligne." | ";
> }
> }
>
> __DATA__
> doing
> close
> sunny
> drugs
> mouve
> botts

This method is perhaps clearer:

----

#!/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;
$contains_all_carac_available &= ($ligne =~ /$_/) for split //,
$carac_available;
if ($contains_all_carac_available && ($ligne !~
/[$carac_notavailable]/)) {
print $ligne." | ";
}
}

__DATA__
doing
close
sunny
drugs
mouve
botts
From: Truty on
David Squire a pens? tr?s fort :
> David Squire wrote:
>
>> What you want is more difficult. Off the top of my head, I can't think of a
>> single regex that would do it (but I guess someone else will :) ). How
>> about this:
>>
>> ----
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $carac_available = 'eo';
>> my $carac_notavailable = 'hydngp';
>>
>> while (my $ligne = <DATA>) {
>> chomp $ligne;
>> my $carac_available_copy = $carac_available;
>> while ($carac_available_copy && $ligne =~ /([$carac_available_copy])/)
>> {
>> $carac_available_copy =~ s/$1//g;
>> }
>> if (!$carac_available_copy && ($ligne !~ /[$carac_notavailable]/)) {
>> print $ligne." | ";
>> }
>> }
>>
>> __DATA__
>> doing
>> close
>> sunny
>> drugs
>> mouve
>> botts
>
> This method is perhaps clearer:
>
> ----
>
> #!/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;
> $contains_all_carac_available &= ($ligne =~ /$_/) for split //,
> $carac_available;
> if ($contains_all_carac_available && ($ligne !~
> /[$carac_notavailable]/)) {
> print $ligne." | ";
> }
> }
>
> __DATA__
> doing
> close
> sunny
> drugs
> mouve
> botts

THANKS ! ! !

It is working very well but I have just an error if $carac_available is
empty or $carac_notavailable.

THANKS :D