Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

If statement multiple conditions

by kbone (Novice)
on Mar 13, 2013 at 22:56 UTC ( #1023318=perlquestion: print w/replies, xml ) Need Help??
kbone has asked for the wisdom of the Perl Monks concerning the following question:


I am looking for some help on something that is likely fairly obvious. I want to match two variables in order for my program to progress. The first match is simple, just matching a form type. The second match is what is confusing me. I am trying to ensure that a given variable is in an array. My current statement looks like this:

 if($form_type=~/$formget/i && $cik~~@ciks)

This code will not parse through my array and return matches. Any suggestions? I am sure that this is largely just a syntax thing but I am stumped. Thanks!

UPDATE: Thank you all for your kindness and fast responses! I was able to figure this out by just looping through my @ciks array - this is probably somewhat inefficient, but the array only has about 300 elements, so it is not too big of a deal.

Replies are listed 'Best First'.
Re: If statement multiple conditions
by choroba (Bishop) on Mar 13, 2013 at 23:11 UTC
    Works for me. Can you show what really is in $cik and @ciks, for example by inserting the following before the if line:
    use Data::Dumper; $Data::Dumper::Useqq = 1; warn Dumper $cik, \@ciks;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thank you for your quick reply! The @ciks array is one I created from a text file. I have printed it at certain points in the program and it seems to work. The $cik variable is one that is generated in the program. Here is the full story: I have a list of identifiers whose SEC filings I want to download. If I do not specifically select those firms, I have to download the entire list from that server, which takes several hours per year. The way this process works is that there is an index file that lists every file available for a given fiscal quarter. In essence, the $cik is the firm identifier from the index file. I only want a certain file type from certain firms...this is what I am trying to match.

      When I use that code you provided when you asked what was in $ciks and @ciks it runs without stopping...seems to repeat over and over again what is in the @ciks array. Thanks again for your help!

      If I use $cik==@ciks[0], I get the single match I am looking for. I am sure this is a minor error that I am just not seeing.

        == is used to compare numbers in Perl. If your data contain strings, you should use eq instead.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: If statement multiple conditions
by Kenosis (Priest) on Mar 13, 2013 at 23:55 UTC

    Are there any meta-characters in $formget that may interfere with the match? If so, consider trying $form_type=~/\Q$formget\E/

      The form type variable does have a .* in it. I tried your suggestion and I still could not pull any matches. I think it must have something to do with how I have set up the array. Thanks for your response!

        You're most welcome, kbone!

Re: If statement multiple conditions
by sundialsvc4 (Abbot) on Mar 14, 2013 at 01:45 UTC

    There are a couple of things that I do “routinely,” partly (but also, specifically) to guard against those WTF?!?! situations ...

    First, in every script, I use use strict; use warnings;.   If you’ve omitted these, add them, run it again and see what happens.

    Second, I will either use nested-if statements, or, when doubling up several conditions, I put each one in a separate set of parentheses whether I think I need them or not.

    I candidly confess that I intentionally do not use “smart matching” (~~) either, although I won’t say that you should or shouldn’t.   I just prefer code that says as plainly as possible (to my feeble old brain) what it means, versus code that says what it says and depends on context to provide what it means.   If I have to read several paragraphs of perldoc perlsyn to fathom what something means, and to be warned that Perl-6 will be different, I don’t do it that way.

      Thanks for the reply!

Re: If statement multiple conditions (stumped? checklist)
by Anonymous Monk on Mar 14, 2013 at 00:20 UTC
Re: If statement multiple conditions
by aitap (Curate) on Mar 14, 2013 at 19:11 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023318]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-05-27 14:46 GMT
Find Nodes?
    Voting Booth?