Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Testing against a list of values ?

Reply
Thread Tools

Testing against a list of values ?

 
 
- Bob -
Guest
Posts: n/a
 
      04-04-2007
I coded the following, which works but is lengthy:

if ( ($temp ne "a")
and( $temp ne "x" )
and ( $temp ne "y" ) ){ ....}

What's a more Perl-like and compact way to code something like this ?


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-04-2007
- Bob - wrote:
> I coded the following, which works but is lengthy:
>
> if ( ($temp ne "a")
> and( $temp ne "x" )
> and ( $temp ne "y" ) ){ ....}
>
> What's a more Perl-like and compact way to code something like this ?


Check out the grep() function.

perldoc -f grep

if ( grep( $temp eq $_, @values ) == 0 ) { ... }

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      04-05-2007
>>>>> "PG" == Purl Gurl <> writes:

PG> Bob wrote:
>> I coded the following, which works but is lengthy:


>> if ( ($temp ne "a") and( $temp ne "x" )
>> and ( $temp ne "y" ) ){ ....}


>> What's a more Perl-like and compact way to code something like this ?


PG> #!perl

PG> while (<DATA>)
PG> {
PG> if ($_ =~ tr/axy// == 0)
PG> { print $_; }
PG> }

as usual moronzilla gets it wrong. try that code with 'ax' or any string
of length > 1 and at least one of 'axy'. it won't behave as the OP
wants.

the correct answer is to use a hash. plenty of examples in this group so
search it with google.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-05-2007
>>>>> "PG" == Purl Gurl <> writes:

PG> Uri Guttman wrote:
>> Purl Gurl wrote:
>>> Bob wrote:


>>>> I coded the following, which works but is lengthy:


>>>> if ( ($temp ne "a") and( $temp ne "x" )
>>>> and ( $temp ne "y" ) ){ ....}


>>>> What's a more Perl-like and compact way to code something like this ?


>>> #!perl


>>> while (<DATA>)
>>> {
>>> if ($_ =~ tr/axy// == 0)
>>> { print $_; }
>>> }


>> as usual moronzilla gets it wrong. try that code with 'ax' or any string
>> of length > 1 and at least one of 'axy'. it won't behave as the OP
>> wants.


PG> Irrelevant. Your comments do not comply with the originating author's
PG> stated parameters. You are practicing deceit, more succinct, you are
PG> lying to readers which is much in keeping with your years long history
PG> and reputation as being amongst the worst of trolls spreading ignorance
PG> and discontent within this discussion group.

he never stated that the input would always be a single character. you
assumed so in your broken code. so your code is wrong as it usually is
as it doesn't account for bad data. it does account for bad code as it
came from you.

as for trolling and such, when have you last published a cpan module,
gave a talk at a conference, been hired to train people in perl or any
of a number of perl things? not in anyone's lifetime here. so please go
back to your loonie been as you did for a blessed few months.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-05-2007
Uri Guttman wrote:
> the correct answer is to use a hash.


s/the correct/One/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      04-05-2007
On Apr 4, 6:15 pm, Purl Gurl <purlg...@purlgurl.net> wrote:
> Uri Guttman wrote:
> > Purl Gurl wrote:
> >> Uri Guttman wrote:
> >>> Purl Gurl wrote:
> >>>> Bob wrote:

>
> (snipped)
>
>
>
> >>> as usual moronzilla gets it wrong. try that code with 'ax' or any string
> >>> of length > 1 and at least one of 'axy'. it won't behave as the OP
> >>> wants.
> >> Irrelevant. Your comments do not comply with the originating author's
> >> stated parameters. You are practicing deceit, more succinct, you are
> >> lying to readers which is much in keeping with your years long history
> >> and reputation as being amongst the worst of trolls spreading ignorance
> >> and discontent within this discussion group.

> > he never stated that the input would always be a single character. you
> > assumed so in your broken code. so your code is wrong as it usually is
> > as it doesn't account for bad data. it does account for bad code as it
> > came from you.
> > as for trolling and such, when have you last published a cpan module,
> > gave a talk at a conference, been hired to train people in perl or any
> > of a number of perl things? not in anyone's lifetime here. so please go
> > back to your loonie been as you did for a blessed few months.

>
> "loony bin"
>
> Ha! Ha! You are so predictable, such a comical bozo troll!
>
> You are THE laughing stock of the Perl community and have
> been so, for many years.
>


I know I shouldn't interject, however, Uri knows what he is talking
about, you don't. Besides, a simple google search on Uri shows that he
is pretty well respected in the Perl community.


 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-05-2007
- Bob - wrote:
> I coded the following, which works but is lengthy:
>
> if ( ($temp ne "a")
> and( $temp ne "x" )
> and ( $temp ne "y" ) ){ ....}
>
> What's a more Perl-like and compact way to code something like this ?


Depending on your real intend (check only
characters - or longer strings), I'd saythat 'grep'
would be 'perlish' (solution already posted by others)

You could make it 'more descriptive' by wrapping it
into a subroutine, like

...

sub NOT_IN { ! grep $_[0] =~ /$_/, @_[1..@_-1] }


my $temp ="bz";

if ( NOT_IN $temp => qw' a x y ' ) {
print "$temp doesn't contain one\n"
# ....
}

...


Regards

M.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-05-2007
- Bob - schreef:

> I coded the following, which works but is lengthy:
>
> if ( ($temp ne "a")
> and( $temp ne "x" )
> and ( $temp ne "y" ) ){ ....}
>
> What's a more Perl-like and compact way to code something like this ?


For example

/^[axy]$/

or

/^(?:a|x|y)$/

where the latter has room for longer values. You can mix'm too:

if ( $temp =~ m!^(?: [ny] | no | yes )$!x ) {...}

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-05-2007
>>>>> "MD" == Michele Dondi <> writes:

MD> On Thu, 05 Apr 2007 01:58:15 +0200, Gunnar Hjalmarsson
MD> <> wrote:

>> if ( grep( $temp eq $_, @values ) == 0 ) { ... }


MD> unless ( grep $temp eq $_, @values) { ... }

MD> But seriously, in this case hash look up is probably better...

MD> (too bad there's not a syntactically sweet enough way to make an array
MD> or a list into a hash(ref) with, say, undef values. In this case I can
MD> think of

MD> unless ( { map { $_ => 1 } @values}->{$temp} ) { ... } # )

and that rebuilds the anon hash each time which is not nice if it is
called more than once. maybe in a cgi or single shot script it would be
ok.

a simple way to make a hash of keys with undef values is:

my %isa_foo ;
@isa_foo{ @values } = () ;

i think i have seen tricks (randal?) on how to merge those line and i
bet abigail or damian could do it with an anon hash. but that is getting
to wacky for my taste. whenever i have a list of things to test against,
i usually need the list as an array too so making an array of foo and a
hash of isa_foo is best IMO (michele: see, i shifted there!)

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-05-2007
>>>>> "MD" == Michele Dondi <> writes:

MD> On Thu, 05 Apr 2007 10:54:28 -0400, Uri Guttman <>
MD> wrote:

>> and that rebuilds the anon hash each time which is not nice if it is
>> called more than once. maybe in a cgi or single shot script it would be
>> ok.
>>
>> a simple way to make a hash of keys with undef values is:
>>
>> my %isa_foo ;
>> @isa_foo{ @values } = () ;


MD> Yes, yes, yes. I don't feel a *compelling* need for such a beast as
MD> that I hinted to in the other post. But occasionally I miss it.
MD> Precisely when I *want* to check if if a single value is in a list,
MD> and want to do so *only once*, possibly in one *single statement*.

MD> Well, a situation that springs to mind is this: the other day I wanted
MD> to check if a coderef is lvalue'd but attributes::get() returns a list
MD> of the attributes defined on a ref, so I'm exactly under the
MD> circumstances described above. Basically I may want something short
MD> enough to be used as in

MD> if ( islvalued($ref) ) { ... }

then List::Util::first is your friend. building a temp hash from a list
and then looking it up is at least O(N) (more caps!) as it has to scan
all the keys. first is O(N) but will scan on average only half of the
keys.

if ( first { $_ eq 'lvalue' } attributes::get($ref) ) { ... }

the hash test is almost always better when you check it more than
once. and as i said i use it if i have the list of keys in advance since
saying $isa_foo{$key} is nice and readable.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing `this' against 0 in non-static member function Shen Weihui C++ 3 06-20-2011 09:07 AM
testing xml against xpather with firefox bruce Python 0 02-16-2009 02:58 AM
Testing against an api Brent Collier Ruby 3 01-24-2009 12:59 AM
M$ against Blu-ray, M$ for Blu-ray, M$ against Blu-ray, M$ forBlu-ray, ...... Blig Merk DVD Video 66 04-27-2008 04:46 AM
Testing (in)equality against a const float Michael Klatt C++ 2 05-21-2004 11:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57