[28313] in Perl-Users-Digest
Perl-Users Digest, Issue: 9677 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 2 14:10:16 2006
Date: Sat, 2 Sep 2006 11:10:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 2 Sep 2006 Volume: 10 Number: 9677
Today's topics:
words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <mgarrish@gmail.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Re: words selection <my_email@no_spam.com>
Re: words selection <my_email@no_spam.com>
Re: words selection <David.Squire@no.spam.from.here.au>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 02 Sep 2006 16:42:32 +0200
From: Truty <my_email@no_spam.com>
Subject: words selection
Message-Id: <mn.13ea7d69b9b67d6b.52132@nospam.com>
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;
Please, can you please me ?
------------------------------
Date: Sat, 02 Sep 2006 16:04:45 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edc6id$csa$1@gemini.csx.cam.ac.uk>
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
------------------------------
Date: Sat, 02 Sep 2006 17:14:57 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.140a7d69af8ae184.52132@nospam.com>
Thanks David,
I try to select more code for help me but I can't to copy and paste all
code.
Wait ... ;)
------------------------------
Date: Sat, 02 Sep 2006 16:17:15 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edc79r$e1r$1@gemini.csx.cam.ac.uk>
Truty wrote:
> Thanks David,
>
>
> I try to select more code for help me but I can't to copy and paste all
> code.
Please quote context when you reply. See the posting guidelines for this
group, which are posted here regularly.
DS
------------------------------
Date: Sat, 02 Sep 2006 17:32:07 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.141c7d6947bf1cae.52132@nospam.com>
Truty avait soumis l'idée :
> 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;
>
>
>
> Please, can you please me ?
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;
I hope to help me ...
------------------------------
Date: Sat, 02 Sep 2006 16:42:04 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edc8oc$gs1$1@gemini.csx.cam.ac.uk>
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
------------------------------
Date: Sat, 02 Sep 2006 16:48:51 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edc953$hhq$1@gemini.csx.cam.ac.uk>
David Squire wrote:
> Truty wrote:
[snip]
>> if (($ligne =~ \[$carac_available]\) && ($ligne !~
>> \[$carac_notavailable]\) ) { print $ligne." | "; }
[snip]
> This will not even compile. You can't use backslashes as regex
> delimiters. The compiler should have told you that.
Clarification: you can't use backslashes as the delimiters without an
operator such as "m" being used at the start, e.g.
if (($ligne =~ /[$carac_available]/) && ($ligne !~
/[$carac_notavailable]/) ) { print $ligne." | "; }
is OK, and so is
if (($ligne =~ m\[$carac_available]\]) && ($ligne !~
m\[$carac_notavailable]\) ) { print $ligne." | "; }
DS
------------------------------
Date: Sat, 02 Sep 2006 16:49:50 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edc96u$hhq$2@gemini.csx.cam.ac.uk>
David Squire wrote:
> David Squire wrote:
>> Truty wrote:
>
> [snip]
>
>>> if (($ligne =~ \[$carac_available]\) && ($ligne !~
>>> \[$carac_notavailable]\) ) { print $ligne." | "; }
>
> [snip]
>
>> This will not even compile. You can't use backslashes as regex
>> delimiters. The compiler should have told you that.
>
> Clarification: you can't use backslashes as the delimiters without an
> operator such as "m" being used at the start, e.g.
>
> if (($ligne =~ /[$carac_available]/) && ($ligne !~
> /[$carac_notavailable]/) ) { print $ligne." | "; }
>
> is OK, and so is
>
> if (($ligne =~ m\[$carac_available]\]) && ($ligne !~
> m\[$carac_notavailable]\) ) { print $ligne." | "; }
Arggh. That should be
if (($ligne =~ m\[$carac_available]]\) && ($ligne !~
m\[$carac_notavailable]\) ) { print $ligne." | "; }
------------------------------
Date: Sat, 02 Sep 2006 17:50:26 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.142e7d6984d168da.52132@nospam.com>
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
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) && ($line !~ $carac_notavailable)) {
print $line." | "; # display word available
}
}
close FILE;
------------------------------
Date: 2 Sep 2006 08:54:38 -0700
From: "Matt Garrish" <mgarrish@gmail.com>
Subject: Re: words selection
Message-Id: <1157212478.458930.178640@b28g2000cwb.googlegroups.com>
David Squire wrote:
> Truty wrote:
>
> > while ($ligne = <FILE_ANSWER>) {
> > chomp($ligne);
> > if (($ligne =~ \[$carac_available]\) && ($ligne !~
> > \[$carac_notavailable]\) ) { print $ligne." | "; }
>
> This will not even compile. You can't use backslashes as regex
> delimiters. The compiler should have told you that.
>
You can't use backslashes as a delimiter without specifying m\\, but
that's true for all non-standard delimiters.
Matt
------------------------------
Date: Sat, 02 Sep 2006 17:06:30 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edca66$jgk$1@gemini.csx.cam.ac.uk>
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
------------------------------
Date: Sat, 02 Sep 2006 18:24:37 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.14507d697019925b.52132@nospam.com>
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.
------------------------------
Date: Sat, 02 Sep 2006 17:55:47 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edcd2j$or4$1@gemini.csx.cam.ac.uk>
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
------------------------------
Date: Sat, 02 Sep 2006 18:00:20 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edcdb4$p65$1@gemini.csx.cam.ac.uk>
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
------------------------------
Date: Sat, 02 Sep 2006 19:09:33 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.147d7d69dfeb81e0.52132@nospam.com>
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
------------------------------
Date: Sat, 02 Sep 2006 18:17:58 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edcec7$r75$1@gemini.csx.cam.ac.uk>
Truty wrote:
> David Squire a pensé très fort :
>>
>> 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.
Then just test for that at the start of the loop...
> THANKS :D
>
>
De rien. Je cherchais quelquechose a faire cet apres-midi gris :)
DS
------------------------------
Date: Sat, 02 Sep 2006 19:26:19 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.148e7d697d944bec.52132@nospam.com>
David Squire a formulé ce samedi :
> Truty wrote:
>> David Squire a pensé très fort :
>>>
>>> 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.
>
> Then just test for that at the start of the loop...
>
>> THANKS :D
>>
>>
>
> De rien. Je cherchais quelquechose a faire cet apres-midi gris :)
>
>
> DS
lol je suis pas une star en anglais et tu parle français Mdr
ba je te remercie ça donne un coup de pouce au projet, qui depuis qlq
jours étaient bloqué à cause de ça.
Merci encore !
------------------------------
Date: Sat, 02 Sep 2006 19:52:18 +0200
From: Truty <my_email@no_spam.com>
Subject: Re: words selection
Message-Id: <mn.14a87d69fa1648cf.52132@nospam.com>
Truty a exposé le 02/09/2006 :
> David Squire a formulé ce samedi :
>> Truty wrote:
>>> David Squire a pensé très fort :
>>>>
>>>> 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.
>>
>> Then just test for that at the start of the loop...
>>
>>> THANKS :D
>>>
>>>
>>
>> De rien. Je cherchais quelquechose a faire cet apres-midi gris :)
>>
>>
>> DS
>
> lol je suis pas une star en anglais et tu parle français Mdr
>
> ba je te remercie ça donne un coup de pouce au projet, qui depuis qlq jours
> étaient bloqué à cause de ça.
>
>
> Merci encore !
if $carac_available is empty or $carac_notavailable too. :s
while (my $ligne = <DATA>) {
chomp $ligne;
if ($carac_available not eq '') {
my $contains_all_carac_available = 1;
$contains_all_carac_available &= ($ligne =~ /$_/) for split
//,
$carac_available;
}
if ($carac_notavailable not eq '') {
if ($contains_all_carac_available && ($ligne !~
/[$carac_notavailable]/)) { print $ligne." | "; }
}
}
I haven't got understand your code, can you terminate it ?
------------------------------
Date: Sat, 02 Sep 2006 18:57:10 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: words selection
Message-Id: <edcglm$28h$1@gemini.csx.cam.ac.uk>
Truty wrote:
> Truty a exposé le 02/09/2006 :
>> David Squire a formulé ce samedi :
>>> Truty wrote:
>>>>
>>>> It is working very well but I have just an error if $carac_available
>>>> is empty or $carac_notavailable.
>>>
>>> Then just test for that at the start of the loop...
>>>
[snip]
>
> if $carac_available is empty or $carac_notavailable too. :s
>
> while (my $ligne = <DATA>) {
> chomp $ligne;
> if ($carac_available not eq '') {
> my $contains_all_carac_available = 1;
> $contains_all_carac_available &= ($ligne =~ /$_/) for split //,
> $carac_available;
> }
> if ($carac_notavailable not eq '') {
> if ($contains_all_carac_available && ($ligne !~
> /[$carac_notavailable]/)) { print $ligne." | "; }
> }
> }
>
> I haven't got understand your code, can you terminate it ?
If those variables get modified during the loop, I guess you just want
to move on to the next iteration of the loop if either of them is empty.
You can do this by using this line:
next if !($carac_available && $carac_notavailable);
DS
PS. Please quote only the relevant context when replying, not the whole
post.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9677
***************************************