Solved

multiple 'or''s

Posted on 2004-10-02
11
138 Views
Last Modified: 2010-03-05
Hi,
 
can anyone tell whats wrong here?
this is the output from the debugger  for

 if ($dd && $ee eq ('a||'b'||'c'||'d')) { dosomething};

when ee equaled c the xpression ee eq ('a||'b'||'c'||'d') still evaluated to false?
when ee was a and b it evaluated correctly....
whats wrong here?


heres the o/p from debugger:

DB<5> x  ($dd eq 'a' && $ee eq ('a'||'b'||'c'||'d'))
0  ''
  DB<6> x  dd
0  'a'
  DB<7>
  DB<8> $ee

  DB<9> x $ee
0  'c'
 
  DB<14> x  ($ee eq ('a'||'b'||'c'||'d'))
0  ''
  DB<15>
0
Comment
Question by:Vlearns
  • 5
  • 4
  • 2
11 Comments
 

Author Comment

by:Vlearns
ID: 12210003
it didnt worlk for ee equals d or b
it works only for a

how to define this such that it is true when either of a b c or d is true



0
 

Author Comment

by:Vlearns
ID: 12210035
how to define a expression such that value is true if ee equals one of a, b,c, or d
0
 
LVL 18

Assisted Solution

by:kandura
kandura earned 200 total points
ID: 12210044
Do you know what ('a'||'b'||'c'||'d') evaluates to?
hint: try "print ('a'||'b'||'c'||'d')"...

What you want is a regular expression:

    $ee =~ /^a|b|c|d$/
0
 
LVL 18

Expert Comment

by:kandura
ID: 12210051
or
    $ee =~ /^[a-d]$/
0
 

Author Comment

by:Vlearns
ID: 12210075
yes it evaluates to first a
i wrote to ((ee eq a)||(ee eq b) || () ||())

to correct that but looking for a better sol
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Vlearns
ID: 12210089

or operators in perl dont worlk loike in C
not very familiar with perl

in ur soln  $ee =~ /^a|b|c|d$/


^ means matches at the start and $ means matching at end?
in general if i need $ee =~/^ant/man/sky/earth$/

will this match if ee is one of these?



the dollar towards the end means
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 12210112
$ee eq 'a' ||$ee eq 'b'||$ee eq 'c'||$ee eq 'd'

$ee =~ /^(a|b|c|d)$/;

grep{$_ eq $ee}qw(a b c d)

${{a=>1,b=>1,c=>1,d=>1}}{$ee}
0
 
LVL 84

Expert Comment

by:ozo
ID: 12210134
$ee="trotskyism";
$ee =~/^ant|man|sky|earth$/
will match

You must use
$ee =~/^(ant|man|sky|earth)$/
to match "sky" exactly.
0
 
LVL 84

Expert Comment

by:ozo
ID: 12210202
Actually
$ee =~/^(ant|man|sky|earth)$/
will also match "ant\n" or "man\n" or "sky\n" or "earth\n"
better to use
$ee =~/\A(ant|man|sky|earth)\z/
see
perldoc perlre
0
 
LVL 84

Expert Comment

by:ozo
ID: 12213215
In Perl6 you will be able to say
  if( $ee eq any('a'..'d') ){
You can also use that construct in Perl5 if you
   use Quantum::Superpositions;
0
 

Author Comment

by:Vlearns
ID: 12273855
Thanks folks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now