Check if words are in uppercase?

Check if words are in uppercase?

am 09.06.2011 09:59:34 von Beware

Hi all,

i've a question on my perl script.

In my script i read a file line per line, and check if keywords are in
uppercase. To do that, i've an array filled with all used keywords.

On each line, i check all keywords with a foreach loop.

Well, this is my code :


my $lines = 0;

while ( )
{
# cut '\n'
chomp($_);

#List of keywords
my @keywords = ("all", "wait", "for");

#Check all keyword
foreach $item (@keywords)
{
# keywords detected
if ( /$item\b/i and !/\s*--/)
{
# remove keywords already in uppercase
my $temp = $_;
my $item_maj = uc($item);
$temp =~ s/$item_maj//g;

# check if any keywords
if ( $temp =~ /$item\b/i )
{
print "keywords is lowercase line : ".$lines."\n";
last;
}
}
}
$lines++;
}
close ( SOURCE );

Well, thanks for your answer


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Check if words are in uppercase?

am 10.06.2011 17:25:30 von Rob Coops

--0016e64ea22a3fb58904a55d2f41
Content-Type: text/plain; charset=UTF-8

On Thu, Jun 9, 2011 at 9:59 AM, Beware wrote:

> Hi all,
>
> i've a question on my perl script.
>
> In my script i read a file line per line, and check if keywords are in
> uppercase. To do that, i've an array filled with all used keywords.
>
> On each line, i check all keywords with a foreach loop.
>
> Well, this is my code :
>
>
> my $lines = 0;
>
> while ( )
> {
> # cut '\n'
> chomp($_);
>
> #List of keywords
> my @keywords = ("all", "wait", "for");
>
> #Check all keyword
> foreach $item (@keywords)
> {
> # keywords detected
> if ( /$item\b/i and !/\s*--/)
> {
> # remove keywords already in uppercase
> my $temp = $_;
> my $item_maj = uc($item);
> $temp =~ s/$item_maj//g;
>
> # check if any keywords
> if ( $temp =~ /$item\b/i )
> {
> print "keywords is lowercase line : ".$lines."\n";
> last;
> }
> }
> }
> $lines++;
> }
> close ( SOURCE );
>
> Well, thanks for your answer
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
Why make you life so hard?

Lets assume you will want some debug logging to know what where and how
often you replaced things...

while ( $line =~ /\b(?'keyword'keyword1|keyword2|keyword3|etc...)\b/i ) {
print debug information ($+{keyword} contains the matched keyword);
$line =~ /$+{keyword}/\U$+{keyword}\E/;
}

This will do the same as your for loop but it will use a single regular
expression that gets used only once if only a single keyword is present in
the line and never more often then the number of keywords in the line being
processed.

If you want to use an external array with keywords simply use that by doing
the following:

my $keywords = join '|', @keywords;
while ( $line =~ /\b(?'keyword'$keywords)\b/i ) { ... }

Regards,

Rob

--0016e64ea22a3fb58904a55d2f41--

Re: Check if words are in uppercase?

am 10.06.2011 19:49:10 von jwkrahn

Beware wrote:
> Hi all,

Hello,

> i've a question on my perl script.
>
> In my script i read a file line per line, and check if keywords are in
> uppercase. To do that, i've an array filled with all used keywords.
>
> On each line, i check all keywords with a foreach loop.
>
> Well, this is my code :
>
>
> my $lines = 0;

You don't really need this variable, just use the built-in $. variable.


> while ( )
> {
> # cut '\n'
> chomp($_);
>
> #List of keywords
> my @keywords = ("all", "wait", "for");

You don't really need this variable inside the while loop.


> #Check all keyword
> foreach $item (@keywords)
> {
> # keywords detected
> if ( /$item\b/i and !/\s*--/)

You should probably have anchors at the beginning as well as the end:

if ( /\b$item\b/i && !/\s*--/ )


> {
> # remove keywords already in uppercase
> my $temp = $_;
> my $item_maj = uc($item);
> $temp =~ s/$item_maj//g;

No need for the $item_maj variable and you should use the anchors here
as well:

$temp =~ s/\b\U$item\E\b//g;


> # check if any keywords
> if ( $temp =~ /$item\b/i )
> {
> print "keywords is lowercase line : ".$lines."\n";
> last;
> }
> }
> }
> $lines++;
> }
> close ( SOURCE );



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Check if words are in uppercase?

am 10.06.2011 21:51:13 von Rob Dixon

On 09/06/2011 08:59, Beware wrote:
> Hi all,
>
> i've a question on my perl script.
>
> In my script i read a file line per line, and check if keywords are in
> uppercase. To do that, i've an array filled with all used keywords.
>
> On each line, i check all keywords with a foreach loop.
>
> Well, this is my code :
>
>
> my $lines = 0;
>
> while ( )
> {
> # cut '\n'
> chomp($_);
>
> #List of keywords
> my @keywords = ("all", "wait", "for");
>
> #Check all keyword
> foreach $item (@keywords)
> {
> # keywords detected
> if ( /$item\b/i and !/\s*--/)
> {
> # remove keywords already in uppercase
> my $temp = $_;
> my $item_maj = uc($item);
> $temp =~ s/$item_maj//g;
>
> # check if any keywords
> if ( $temp =~ /$item\b/i )
> {
> print "keywords is lowercase line : ".$lines."\n";
> last;
> }
> }
> }
> $lines++;
> }
> close ( SOURCE );
>
> Well, thanks for your answer

Erm, what is your question?! I am not clear what the purpose of your
program is, but you seem to printing the numbers of data lines that
contain any of a list of keywords in lower case. To do that there is no
need to remove upper case occurrences at all - simply look only for
lower case strings. The program below does just that, by first
building a regex from the keywords array.

Do consider how you want to handle keywords of mixed case, like 'For' or
'All'.

HTH,

Rob


use strict;
use warnings;

my @keywords = qw(all wait for);
my $key_re = join '|', @keywords;
$key_re = qr/\b(?:$key_re)\b/o;

while (my $line = ) {
if ($line =~ $key_re) {
print "keywords is lowercase line : $.\n";
}
}

__DATA__
all
wine WAITER
wait
FOR
for

**OUTPUT**

keywords is lowercase line : 2
keywords is lowercase line : 4
keywords is lowercase line : 6

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Check if words are in uppercase?

am 13.06.2011 10:11:22 von Beware

Hi,

Sorry, i forgot to write my question clearly. But you well understand my=20
problem :)
Well, i try your answer, and it seems matching my request. Thanks you=20
very much.

Le 10/06/2011 21:51, Rob Dixon a =E9crit :
> On 09/06/2011 08:59, Beware wrote:
>> Hi all,
>>
>> i've a question on my perl script.
>>
>> In my script i read a file line per line, and check if keywords are in
>> uppercase. To do that, i've an array filled with all used keywords.
>>
>> On each line, i check all keywords with a foreach loop.
>>
>> Well, this is my code :
>>
>>
>> my $lines =3D 0;
>>
>> while ( )
>> {
>> # cut '\n'
>> chomp($_);
>>
>> #List of keywords
>> my @keywords =3D ("all", "wait", "for");
>>
>> #Check all keyword
>> foreach $item (@keywords)
>> {
>> # keywords detected
>> if ( /$item\b/i and !/\s*--/)
>> {
>> # remove keywords already in uppercase
>> my $temp =3D $_;
>> my $item_maj =3D uc($item);
>> $temp =3D~ s/$item_maj//g;
>>
>> # check if any keywords
>> if ( $temp =3D~ /$item\b/i )
>> {
>> print "keywords is lowercase line : ".$lines."\n";
>> last;
>> }
>> }
>> }
>> $lines++;
>> }
>> close ( SOURCE );
>>
>> Well, thanks for your answer
>
> Erm, what is your question?! I am not clear what the purpose of your
> program is, but you seem to printing the numbers of data lines that
> contain any of a list of keywords in lower case. To do that there is no
> need to remove upper case occurrences at all - simply look only for
> lower case strings. The program below does just that, by first
> building a regex from the keywords array.
>
> Do consider how you want to handle keywords of mixed case, like 'For' o=
r
> 'All'.
>
> HTH,
>
> Rob
>
>
> use strict;
> use warnings;
>
> my @keywords =3D qw(all wait for);
> my $key_re =3D join '|', @keywords;
> $key_re =3D qr/\b(?:$key_re)\b/o;
>
> while (my $line =3D ) {
> if ($line =3D~ $key_re) {
> print "keywords is lowercase line : $.\n";
> }
> }
>
> __DATA__
> all
> wine WAITER
> wait
> FOR
> for
>
> **OUTPUT**
>
> keywords is lowercase line : 2
> keywords is lowercase line : 4
> keywords is lowercase line : 6


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Check if words are in uppercase?

am 14.06.2011 09:30:59 von Beware

@John : Thank you for your advices.

@Rob Dixon : Your script works fine, but i want to add a little more
thing. In fact, i want to detect if a word is in line (of course) and
if it's not in uppercase.
I mean, for example, i want to detect :
aLL
aLl
All
etc..
but not ALL (which is correct for me).

Thanks.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Check if words are in uppercase?

am 14.06.2011 16:40:14 von Jim Gibson

At 12:30 AM -0700 6/14/11, Beware wrote:
>@John : Thank you for your advices.
>
>@Rob Dixon : Your script works fine, but i want to add a little more
>thing. In fact, i want to detect if a word is in line (of course) and
>if it's not in uppercase.
>I mean, for example, i want to detect :
>aLL
>aLl
>All
>etc..
>but not ALL (which is correct for me).

if( $word =~ /^all$/i && $word ne 'ALL' )


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re : Re: Check if words are in uppercase?

am 14.06.2011 16:47:04 von Beware

Hi and thank you for your answer.

But how can i use it with a list of word.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Check if words are in uppercase?

am 14.06.2011 17:53:18 von Jim Gibson

On 6/14/11 Tue Jun 14, 2011 7:47 AM, "Beware"
scribbled:

> Hi and thank you for your answer.
>
> But how can i use it with a list of word.
>

Use what? You need to put a little context in your messages so people can
help you without seeing previous messages.


If you have a list of words in an array (@words) and you want to see if some
string ($string) matches those words except for case and is not all
upper-case, there are several possibilities.

1. You can test if a string consists of only upper-case letters:

if( $string =~ /^[A-Z]+$/ )

2. You can test if $string matches any of the words in @words in
case-insensitive fashion:


for my $word ( @words ) {
if( $string =~ /^$word$/i ) {
# string matches
last; # no need for further tests
}
}

3. You can do both:

for my $word ( @words ) {
if( $string =~ /^$word$/i && $string ne uc $word ) {
# string matches
last; # no need for further tests
}
}

(You can also set all members of @word to upper-case to begin with.)

What exactly are you trying to do? Can you provide a Perl program that
illustrates this?



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 08:56:04 von Beware

Hi

Sorry, i've been tired these past days.

So, this is what i want to do :

I've source code files in VHDL. I want to check that all specific keywords of this language are in uppercase.

In fact, in my current script i read this file line per line and check others rules.

Am i clear, now?


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re :Re: Checkif words are in uppercase?

am 15.06.2011 13:10:56 von Paul Johnson

On Tue, Jun 14, 2011 at 11:56:04PM -0700, Beware wrote:
> Hi
>
> Sorry, i've been tired these past days.
>
> So, this is what i want to do :
>
> I've source code files in VHDL. I want to check that all specific keywords of this language are in uppercase.
>
> In fact, in my current script i read this file line per line and check others rules.
>
> Am i clear, now?

Here is some code that does this. I suggest you make sure you understand what
it does and check that it is correct. Call it by passing the names of your
VHDL files.

Note that this code isn't very clever. In particular you will get false
positives. To do the job properly you would need a VHDL parser. For that
reason I would suggest against modifying the code to alter the input file as
part of a checkin hook, for example.

(I prefer my keywords to be in lowercase.)


#!/usr/bin/perl

use strict;
use warnings;

my @keywords = qw
(
abs access after alias all and architecture array assert attribute begin
block body buffer bus case component configuration constant disconnect
downto else elsif end entity exit file for function generate generic group
guarded if impure in inertial inout is label library linkage literal loop
map mod nand new next nor not null of on open or others out package port
postponed procedure process pure range record register reject return rol
ror select severity signal shared sla sli sra srl subtype then to
transport type unaffected units until use variable wait when while with
xnor xor
);

my $kw = join "|", @keywords;

while (<>)
{
for my $w (/(\b$kw\b)/ig)
{
warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
}
}

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 13:38:02 von Rob Dixon

On 15/06/2011 12:10, Paul Johnson wrote:
> On Tue, Jun 14, 2011 at 11:56:04PM -0700, Beware wrote:
>> Hi
>>
>> Sorry, i've been tired these past days.
>>
>> So, this is what i want to do :
>>
>> I've source code files in VHDL. I want to check that all specific keywords of this language are in uppercase.
>>
>> In fact, in my current script i read this file line per line and check others rules.
>>
>> Am i clear, now?
>
> Here is some code that does this. I suggest you make sure you understand what
> it does and check that it is correct. Call it by passing the names of your
> VHDL files.
>
> Note that this code isn't very clever. In particular you will get false
> positives. To do the job properly you would need a VHDL parser. For that
> reason I would suggest against modifying the code to alter the input file as
> part of a checkin hook, for example.
>
> (I prefer my keywords to be in lowercase.)
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @keywords = qw
> (
> abs access after alias all and architecture array assert attribute begin
> block body buffer bus case component configuration constant disconnect
> downto else elsif end entity exit file for function generate generic group
> guarded if impure in inertial inout is label library linkage literal loop
> map mod nand new next nor not null of on open or others out package port
> postponed procedure process pure range record register reject return rol
> ror select severity signal shared sla sli sra srl subtype then to
> transport type unaffected units until use variable wait when while with
> xnor xor
> );
>
> my $kw = join "|", @keywords;

You would need

my $kw = join "|", map uc, @keywords;

:)

Rob

> while (<>)
> {
> for my $w (/(\b$kw\b)/ig)
> {
> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
> }
> }
>


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 14:41:39 von jwkrahn

Rob Dixon wrote:
> On 15/06/2011 12:10, Paul Johnson wrote:
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my @keywords = qw
>> (
>> abs access after alias all and architecture array assert attribute begin
>> block body buffer bus case component configuration constant disconnect
>> downto else elsif end entity exit file for function generate generic
>> group
>> guarded if impure in inertial inout is label library linkage literal loop
>> map mod nand new next nor not null of on open or others out package port
>> postponed procedure process pure range record register reject return rol
>> ror select severity signal shared sla sli sra srl subtype then to
>> transport type unaffected units until use variable wait when while with
>> xnor xor
>> );
>>
>> my $kw = join "|", @keywords;
>
> You would need
>
> my $kw = join "|", map uc, @keywords;

Or:

my $kw = uc join '|', @keywords;

Why call uc() multiple times when you only need to call it once?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 17:02:15 von Jim Gibson

At 12:38 PM +0100 6/15/11, Rob Dixon wrote:
>On 15/06/2011 12:10, Paul Johnson wrote:
>>On Tue, Jun 14, 2011 at 11:56:04PM -0700, Beware wrote:
>>>Hi
>>>
>>>Sorry, i've been tired these past days.
>>>
>>>So, this is what i want to do :
>>>
>>>I've source code files in VHDL. I want to check that all specific
>>>keywords of this language are in uppercase.
>>>
>>>In fact, in my current script i read this file line per line and
>>>check others rules.
>>>
>>>Am i clear, now?
>>
>>Here is some code that does this. I suggest you make sure you
>>understand what
>>it does and check that it is correct. Call it by passing the names of your
>>VHDL files.
>>
>>Note that this code isn't very clever. In particular you will get false
>>positives. To do the job properly you would need a VHDL parser. For that
>>reason I would suggest against modifying the code to alter the input file as
>>part of a checkin hook, for example.
>>
>>(I prefer my keywords to be in lowercase.)
>>
>>
>>#!/usr/bin/perl
>>
>>use strict;
>>use warnings;
>>
>>my @keywords = qw
>>(
>> abs access after alias all and architecture array assert attribute begin
>> block body buffer bus case component configuration constant disconnect
>> downto else elsif end entity exit file for function generate
>>generic group
>> guarded if impure in inertial inout is label library linkage
>>literal loop
>> map mod nand new next nor not null of on open or others out package port
>> postponed procedure process pure range record register reject return rol
>> ror select severity signal shared sla sli sra srl subtype then to
>> transport type unaffected units until use variable wait when while with
>> xnor xor
>>);
>>
>>my $kw = join "|", @keywords;
>
>You would need
>
> my $kw = join "|", map uc, @keywords;


Why? He is using the 'i' modifier in the regular expression to find
all keywords regardless of case (lower, upper, mixed). Then checking
to see if the extracted keyword is equal to the upper-case version
and printing a warning message if it is not.

>
>>while (<>)
>>{
>> for my $w (/(\b$kw\b)/ig)
>> {
>> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
>> }
>>}
>>


--
Jim Gibson
Jim@Gibson.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 17:20:47 von Rob Dixon

On 15/06/2011 16:02, Jim Gibson wrote:
> At 12:38 PM +0100 6/15/11, Rob Dixon wrote:
>> On 15/06/2011 12:10, Paul Johnson wrote:
>>> On Tue, Jun 14, 2011 at 11:56:04PM -0700, Beware wrote:
>>>> Hi
>>>>
>>>> Sorry, i've been tired these past days.
>>>>
>>>> So, this is what i want to do :
>>>>
>>>> I've source code files in VHDL. I want to check that all specific
>>>> keywords of this language are in uppercase.
>>>>
>>>> In fact, in my current script i read this file line per line and
>>>> check others rules.
>>>>
>>>> Am i clear, now?
>>>
>>> Here is some code that does this. I suggest you make sure you
>>> understand what
>>> it does and check that it is correct. Call it by passing the names of
>>> your
>>> VHDL files.
>>>
>>> Note that this code isn't very clever. In particular you will get false
>>> positives. To do the job properly you would need a VHDL parser. For that
>>> reason I would suggest against modifying the code to alter the input
>>> file as
>>> part of a checkin hook, for example.
>>>
>>> (I prefer my keywords to be in lowercase.)
>>>
>>>
>>> #!/usr/bin/perl
>>>
>>> use strict;
>>> use warnings;
>>>
>>> my @keywords = qw
>>> (
>>> abs access after alias all and architecture array assert attribute begin
>>> block body buffer bus case component configuration constant disconnect
>>> downto else elsif end entity exit file for function generate generic
>>> group
>>> guarded if impure in inertial inout is label library linkage literal
>>> loop
>>> map mod nand new next nor not null of on open or others out package port
>>> postponed procedure process pure range record register reject return rol
>>> ror select severity signal shared sla sli sra srl subtype then to
>>> transport type unaffected units until use variable wait when while with
>>> xnor xor
>>> );
>>>
>>> my $kw = join "|", @keywords;
>>
>> You would need
>>
>> my $kw = join "|", map uc, @keywords;
>
>
> Why? He is using the 'i' modifier in the regular expression to find all
> keywords regardless of case (lower, upper, mixed). Then checking to see
> if the extracted keyword is equal to the upper-case version and printing
> a warning message if it is not.

Very true: I missed the uc within the loop and the code is fine as it
stands. But it would be better to write, as John said

my $kw = uc join "|", @keywords;

and drop the call inside the loop.

warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq $w;

>>
>>> while (<>) {
>>> for my $w (/(\b$kw\b)/ig) {
>>> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
>>> }
>>> }


Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 18:09:01 von Jim Gibson

On 6/15/11 Wed Jun 15, 2011 8:20 AM, "Rob Dixon"
scribbled:

> On 15/06/2011 16:02, Jim Gibson wrote:
>> At 12:38 PM +0100 6/15/11, Rob Dixon wrote:
>>> On 15/06/2011 12:10, Paul Johnson wrote:
>>>> On Tue, Jun 14, 2011 at 11:56:04PM -0700, Beware wrote:
>>>>> Hi
>>>>>
>>>>> Sorry, i've been tired these past days.
>>>>>
>>>>> So, this is what i want to do :
>>>>>
>>>>> I've source code files in VHDL. I want to check that all specific
>>>>> keywords of this language are in uppercase.
>>>>>
>>>>> In fact, in my current script i read this file line per line and
>>>>> check others rules.
>>>>>
>>>>> Am i clear, now?
>>>>
>>>> Here is some code that does this. I suggest you make sure you
>>>> understand what
>>>> it does and check that it is correct. Call it by passing the names of
>>>> your
>>>> VHDL files.
>>>>
>>>> Note that this code isn't very clever. In particular you will get false
>>>> positives. To do the job properly you would need a VHDL parser. For that
>>>> reason I would suggest against modifying the code to alter the input
>>>> file as
>>>> part of a checkin hook, for example.
>>>>
>>>> (I prefer my keywords to be in lowercase.)
>>>>
>>>>
>>>> #!/usr/bin/perl
>>>>
>>>> use strict;
>>>> use warnings;
>>>>
>>>> my @keywords = qw
>>>> (
>>>> abs access after alias all and architecture array assert attribute begin
>>>> block body buffer bus case component configuration constant disconnect
>>>> downto else elsif end entity exit file for function generate generic
>>>> group
>>>> guarded if impure in inertial inout is label library linkage literal
>>>> loop
>>>> map mod nand new next nor not null of on open or others out package port
>>>> postponed procedure process pure range record register reject return rol
>>>> ror select severity signal shared sla sli sra srl subtype then to
>>>> transport type unaffected units until use variable wait when while with
>>>> xnor xor
>>>> );
>>>>
>>>> my $kw = join "|", @keywords;
>>>
>>> You would need
>>>
>>> my $kw = join "|", map uc, @keywords;
>>
>>
>> Why? He is using the 'i' modifier in the regular expression to find all
>> keywords regardless of case (lower, upper, mixed). Then checking to see
>> if the extracted keyword is equal to the upper-case version and printing
>> a warning message if it is not.
>
> Very true: I missed the uc within the loop and the code is fine as it
> stands. But it would be better to write, as John said
>
> my $kw = uc join "|", @keywords;
>
> and drop the call inside the loop.
>
> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq $w;
>

I am afraid that will not work. $w is extracted from the input line and may
contain lower-case letters. You must compare $w to uc $w to see if it
contains any lower-case letters. '$w eq $w' is always true.


>>>
>>>> while (<>) {
>>>> for my $w (/(\b$kw\b)/ig) {
>>>> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
>>>> }
>>>> }



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 15.06.2011 18:57:03 von Rob Dixon

On 15/06/2011 17:09, Jim Gibson wrote:
> On 6/15/11 Wed Jun 15, 2011 8:20 AM, "Rob Dixon" scribbled:
>
> I am afraid that will not work. $w is extracted from the input line and may
> contain lower-case letters. You must compare $w to uc $w to see if it
> contains any lower-case letters. '$w eq $w' is always true.

Yes, for some reason I'm talking nonsense today.

Thanks Jim

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Check if words are in uppercase?

am 17.06.2011 21:57:28 von Brandon McCaig

On Wed, Jun 15, 2011 at 12:09 PM, Jim Gibson wrote:
> '$w eq $w' is always true.

It certainly is more sane when that holds true, but to have a little
fun there is overload. ;D

use strict;
use warnings;

use overload '==' => sub { return 0; };

my $foo = bless {};

print $foo == $foo;

__END__


--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re : Re: Re : Re: Re : Re: Check if words are in up

am 21.06.2011 14:57:22 von Beware

Hi to all,

First of all, sorry for the late of my answer.
Thank for all your sentence.

Here's my solution (for now) :

for my $w (@keywords)
{
if ( /\b$w\b/ and !/\buc($w)\b/ )
{
print "Keyword '$w' not uppercase line $.\n";
ajoute_erreur( 7, $. );
last;
}
}

It's probably less fast than other ones, but it seems to work.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re :Re: Re : Re: Re : Re:Check if words are in uppercase?

am 21.06.2011 15:53:53 von Paul Johnson

On Tue, Jun 21, 2011 at 05:57:22AM -0700, Beware wrote:

> Hi to all,
>
> First of all, sorry for the late of my answer.
> Thank for all your sentence.
>
> Here's my solution (for now) :
>
> for my $w (@keywords)
> {
> if ( /\b$w\b/ and !/\buc($w)\b/ )
> {
> print "Keyword '$w' not uppercase line $.\n";
> ajoute_erreur( 7, $. );
> last;
> }
> }
>
> It's probably less fast than other ones, but it seems to work.

I'm afraid you may need to improve your testing skills.

I assume your keywords are in lower case. What happens with mixed case? You
would need /i on your first //

uc($w) isn't doing what you think it is - you were wanting \U$w\E

But the trouble with your method is that you will miss things like
"a and b AND c" because it *will* find the uppercase version on the line.

Also, you probably don't want the "last" there or you'll only ever find one
error per line.

I refer the right honourable Gentleman to my previous answer.

Good luck,

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Re : Re: Re : Re: Re : Re: Check if

am 21.06.2011 16:01:17 von Bob McConnell

From: Paul Johnson

> On Tue, Jun 21, 2011 at 05:57:22AM -0700, Beware wrote:
>=20
> > Hi to all,
>>=20
>> First of all, sorry for the late of my answer.
>> Thank for all your sentence.
>>=20
>> Here's my solution (for now) :
>>=20
>> for my $w (@keywords)
>> {
>> if ( /\b$w\b/ and !/\buc($w)\b/ )
>> {
>> print "Keyword '$w' not uppercase line $.\n";
>> ajoute_erreur( 7, $. );
>> last;
>> }
>> }
>>=20
>> It's probably less fast than other ones, but it seems to work.
>=20
> I'm afraid you may need to improve your testing skills.
>=20
> I assume your keywords are in lower case. What happens with mixed =
case? You
> would need /i on your first //

You need to be a little more careful about those assumptions. He is =
looking for all keywords that are not in UPPERCASE.

Bob McConnell

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re :Re: Re : Re: Re : Re:Check if words are in uppercase?

am 21.06.2011 16:47:51 von Paul Johnson

On Tue, Jun 21, 2011 at 10:01:17AM -0400, Bob McConnell wrote:
> From: Paul Johnson
>
> > On Tue, Jun 21, 2011 at 05:57:22AM -0700, Beware wrote:
> >
> > > Hi to all,
> >>
> >> First of all, sorry for the late of my answer.
> >> Thank for all your sentence.
> >>
> >> Here's my solution (for now) :
> >>
> >> for my $w (@keywords)
> >> {
> >> if ( /\b$w\b/ and !/\buc($w)\b/ )
> >> {
> >> print "Keyword '$w' not uppercase line $.\n";
> >> ajoute_erreur( 7, $. );
> >> last;
> >> }
> >> }
> >>
> >> It's probably less fast than other ones, but it seems to work.
> >
> > I'm afraid you may need to improve your testing skills.
> >
> > I assume your keywords are in lower case. What happens with mixed case? You
> > would need /i on your first //
>
> You need to be a little more careful about those assumptions. He is looking for all keywords that are not in UPPERCASE.

Sorry, I wasn't clear. What I should have said was something like: I assume
that the elements of the array @keywords are all in lower case.

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Re : Re: Re : Re: Re : Re: Check if words are in uppercase?

am 21.06.2011 19:09:54 von Rob Dixon

On 21/06/2011 15:01, Bob McConnell wrote:
> From: Paul Johnson
>> On Tue, Jun 21, 2011 at 05:57:22AM -0700, Beware wrote:
>>
>>> Hi to all,
>>>
>>> First of all, sorry for the late of my answer.
>>> Thank for all your sentence.
>>>
>>> Here's my solution (for now) :
>>>
>>> for my $w (@keywords)
>>> {
>>> if ( /\b$w\b/ and !/\buc($w)\b/ )
>>> {
>>> print "Keyword '$w' not uppercase line $.\n";
>>> ajoute_erreur( 7, $. );
>>> last;
>>> }
>>> }
>>>
>>> It's probably less fast than other ones, but it seems to work.
>>
>> I'm afraid you may need to improve your testing skills.
>>
>> I assume your keywords are in lower case. What happens with mixed case? You
>> would need /i on your first //
>
> You need to be a little more careful about those assumptions.
> He is looking for all keywords that are not in UPPERCASE.

In fact not, the OP corrected his requirement later on

On 15/06/2011 07:56, Beware wrote:
>
> So, this is what i want to do :
>
> I've source code files in VHDL. I want to check that all specific
> keywords of this language are in uppercase.
>
> In fact, in my current script i read this file line per line and
> check others rules.
>
> Am i clear, now?

The working solution that I prefer is Paul's

On 15/06/2011 12:10, Paul Johnson wrote:
>
> my $kw = join "|", @keywords;
>
> while (<>) {
> for my $w (/(\b${kw}\b)/ig) {
> warn "Keyword '$w' not uppercase at $ARGV:$.\n" unless $w eq uc $w;
> }
> }

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/