Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

alpha vs alpha numeric

by malaga (Pilgrim)
on Jun 12, 2002 at 10:57 UTC ( [id://173758]=perlquestion: print w/replies, xml ) Need Help??

malaga has asked for the wisdom of the Perl Monks concerning the following question:

this is just one line of code, but i think it's where the problem is...maybe. if $value is all alpha it works fine, and returns a list of items, but if it's alpha-numeric it doesn't return anything. is there something obviously wrong with this line? (thank you)

if (($ref->{prodname} =~ ($value)) || ($ref->{key1} =~ ($value)))

Replies are listed 'Best First'.
Re: alpha vs alpha numeric
by svad (Pilgrim) on Jun 12, 2002 at 11:06 UTC
    Do you actually intended to use regular expression?
    I think you intended to use just usual comparision 'eq':
    if (($ref->{prodname} eq ($value)) || ($ref->{key1} eq ($value)))
    Vadim.
Re: alpha vs alpha numeric
by Abigail-II (Bishop) on Jun 12, 2002 at 11:12 UTC
    You are using odd syntax, but that shouldn't be an issue. Normal syntax is
    if ($ref->{prodname} =~ /$value/ || $ref->{key1} =~ /$value/)

    You are saying it returns a list of items, but all the code shows is a conditional. There isn't anything wrong with the line. It's probably "failing" because either the hash or $value contains other values than you expect.

    Could you give an example of a failure, including the values for the hash entries and $value.

    Abigail

Re: alpha vs alpha numeric
by strat (Canon) on Jun 12, 2002 at 11:18 UTC
    in case of failure, maybe $value contains chars like \ or + or the like which are interpolated on the fly. I don't like this standard behaviour of perl, because I prefer enabling special interpretation to disabling special interpretation, and I've seen many people fallen into this trap (included myself).

    if ($ref->{prodname} =~ /\Q$value\E/ or $ref->{key1} =~ /\Q$value/ ) { ...
    might work because \Q prevents interpolation of $variables in patterns until \E (see first pattern) which you can omit if it is at the end of the pattern (see second pattern)

    I decided to use ... or ... instead of (...) || (...) because I think it is easier to read and you can omit a couple of paranteses. But this is just a matter of my personal taste :-) (Beware that || has a higher precedence than "or", so with || you need parantheses in this case...)

    In your example, $ref->{prodname} will contain $value and perhaps some other stuff. If you want to check if $ref->{prodname} is exactly the same as $value, better use $ref->{prodname} eq $value, or if there are only numbers in both, $ref->{prodname} == $value.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: alpha vs alpha numeric
by perigeeV (Hermit) on Jun 12, 2002 at 11:13 UTC

    I don't know what you're comparing with the regex, but you do have to include the preceding "m" in a regex when you chose to use delimiters different than the default slashes, eg.:

    if (($ref->{prodname} =~ m($value)) || ($ref->{key1} =~ m($value)))

    You may also consider testing with eq if you want everything stringified.


      Yeah, if you use different delimiters you have to use the m. But he is not using different delimiters! If you run his code, you'll see it compiles fine. The parenthesis here just play their usual role: indicating precedence. They are not needed, but the don't change the meaning either, just as in: (3) + (4). So, all you have are strings on both sides of the binding operator. And Perl handles that just fine....

      Abigail

Re: alpha vs alpha numeric
by malaga (Pilgrim) on Jun 12, 2002 at 11:27 UTC
    Yes, i'm checking for an exact match. i tried with Q, no difference. there aren't any characters besides alpha and numeric. Thanks again.
      Maybe you could split up the line into two statements to find the problem easier:
      if ($ref->{prodname} =~ /$value/) { # worked } elsif($ref->{key1} =~ /\Q$value\E/) { # worked }
      For the next step, try to replace /\Q$value\E/ with /^\Q$value\E$/<P> If it still works, the only possibility that comes into my mind are newlines (e.g. if you read something from a file or STDIN) in either $ref->{...} or $value. You can remove them with chomp( $ref->{...} ) or the like.

      Then, $ref->{...} eq $value might work as well.

      If you need case insensitive comparisons, try $ref->{...} =~ /^\Q$value\E/i, or in the next step:
      if (lc ( $ref->{...} ) eq lc($value) ) {

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: alpha vs alpha numeric
by malaga (Pilgrim) on Jun 12, 2002 at 11:22 UTC
    thank you all. if i use eq i get no matches. (and adding m didn't change anything). I'll have to examine the rest of the script.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173758]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found