As a background, I write php code that parses csv files and does something with each line of each csv file. That this "something" depends on the values in the string. It is easy to check values using the "if" structure, but hard-coded conditions are not optimal for two reasons:
There are several hundred possible conditions for testing. This is just for starters. Additional terms will be added in the future.
Each csv line does not need to be checked for each condition; once the condition for the string evaluates to true, further conditions need not be evaluated.
Ideally, for my situation, the "if" conditions will be stored in the postgres table, placed in a string variable in turn, and then each variable will be checked by one if structure (inside a loop) until the condition is true.
A simplified example:
$arrayOne[3] = "foo"; // in practice, the value of this variable would not be hard-coded; // it would come from a postgres table $conditionString="\$arrayOne[3] == \"VANILLA\""; if($conditionString) { // do something, then exit the loop this if statement would be // inside of in actual practice }
This question was essentially asked in
PHP - if condition inside string
There were three main answers:
- You can use eval (), but NOT! This evil! (Agreed)
- Hard code of all conditions (not optimal for the reasons above and others)
- Save the conditions in the circuit (this is what I am doing) and then parse and convert to php code.
Solution 3 is usually what I'm looking for, but the second part seems inefficient and unnecessarily complicated. In the end, why create php code from multiple lines (an unknown number, by the way, which makes postgres difficult to store), when it seems a lot easier to just save and then evaluate the line you need?
Is there any way to do this?
Thanks so much for the answers so far. ComFreek, thanks to you in particular for a detailed answer. The solution you offer may be just what I need, but to be honest, I have no experience knowing if this will happen. I will definitely spend time trying to understand what you are saying. Hope this solves my problem.
If this is not the case, and in the meantime, to answer a few questions, others asked:
1) if conditions will usually not be simple. Many of them will contain multiple and complex AND and OR tests. The selection condition in the pseudo-code can be: (field2 == "BUY" AND (strpos ("REINVEST DIVIDEND", field6) OR strpos ("CASH MERGER, field6)) AND field2! =" TTXY "AND field3> 0).
2) CSV files come from numerous financial institutions. They contain a lot of the same information, but each has unique data, and they all have data in different places. The more they express data in different ways. In some cases, payment is indicated by a negative number; in others, a positive number. Some have separate fields for deposits and withdrawals; some indicate deposits and withdrawals with a code in another column. And so on. The code should determine the nature of the transaction (purchase of a credit card, verification, purchase or sale of shares, retirement, etc., etc.), and then, if possible, assign the correct debit / credit account numbers (from the account diagram) to this transaction. In all, there are hundreds of possible conditions that can become thousands. (In case someone is asking a question, the code can determine the institution from which a particular csv file originates, and will verify the transactions in this file only on conditions related to that institution.)
3) The code should be flexible enough to easily (in other words, without writing new code) allow adding new tests in the future. For me, the ability to add a new condition to the postgres table (which will be another test for the code being tested) is quite flexible.
To try to answer Phil's questions and comments (which I may not understand correctly):
1) I know what preg_match is, but I haven’t really studied what it can do, so this may actually be the answer to my problem. I will check it.
2) currently the code does not group transactions (i.e. one line from one csv file); rather, it looks at each row, determines what it is, then saves additional data in the corresponding postgres tables, and then moves on to the next row. There are certain “types” of transactions (such as credit card purchases), but they are never grouped for further processing.
3) Each transaction must satisfy one and only condition (although this condition can be complex).
4) As for matching the whole line, if I don’t miss something (very possible), it is not so simple. For example, suppose a given transaction is a stock purchase. The code can determine that, seeing that the "action" field contains the word "Buy", and the "quantity" field is greater than zero (one or the other of these conditions may not be enough to be sure that the transaction is a purchase of shares), but the “ticker” field can be any of thousands of lines that are not known in advance - “GOOG”, “MSFT”, “KO” or something else.
Again, thanks to everyone for the answers so far.