Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: charachter classes

by merlyn (Sage)
on Aug 21, 2000 at 03:28 UTC ( [id://28749] : note . print w/replies, xml ) Need Help??


in reply to charachter classes, and a misunderstanding...

The second program says yes if there is at least one character not in the class, not if all characters are not in the class.

You want something like:

... if (/^[^yhnujmikolp]+$/i) { ...
which says "from the beginning to the ending, are they all not in this class".

This is a common problem when people negate things too much: the negations stack up in the wrong direction. Me, I avoid that kind of regex, and ask for what I want, which will also be faster:

@ARGV = qw(/usr/dict/words); my $longest = ""; while (<>) { next unless length > length $longest; # too short next if /[^qwertasdfgzxcvb\n]/i; # wrong hand $longest = $_; } print $longest;
And this finds "aftereffect", as you noted. But with a lot less code, as I'm often prone to do. {grin}

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: Re: charachter classes
by mdillon (Priest) on Aug 21, 2000 at 03:43 UTC

    in my dictionary file, there are multiple left-hand-only words sharing the longest length. the following code outputs all of them.

    @ARGV = qw(/usr/dict/words); my $len = 0; my @largest; while (<>) { chomp; next unless length >= $len && /^[qazwsxedcrfvtgb]+$/i; if (length > $len) { @largest = (); $len = length; } push @largest, $_; } print join(', ', @largest), $/;

    this gives the full set of answers: 'aftereffect', 'desegregate', 'exacerbated', 'exacerbates', 'exaggerated', 'exaggerates', 'reverberate', and 'vertebrates'.