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

Hi All looking for help with a script that looks to a csv file to dete +rmine how to parse another file in order to get an end value. Table: #PTC,FTC,QUAN,QSIGN,AMT,ASIGN,ST ADM,RCV,,+,,, ADM,DLV,,,,, ABC,EFG,,,,,8 ABC,XYZ,,-,,,7 for the first example: if field1 ne "" field4 ne "" then I want to build a statement using the 3 fields that are populated +: if ($CODE1 eq "$field1") && ($CODE4 eq "$field4) { $TRN = "$field2"; } if field1 ne "" field7 ne "" if ($CODE1 eq "$field1") && ($CODE7 eq "$field7) { $TRN = "$field2"; } if field1 ne "" if ($CODE1 eq "$field1") { $TRN = "$field2"; } The $CODE* fields are fields I have already parsed from a line in a te +xt file and then if there is a match assign $TRN. Trying to avoid having to right 100s of if statements for each possibl +e scenario. &GetTrnCode($X); sub GetTrnCode ($) { $CODE = ""; $TRANCODE = ""; $PERcode=""; $FDXcode=""; $CODE = $_[0]; my %TrnCode; $data2 = "PerTrnTable.txt"; open(inData2, $data2); while (<inData2>) { chomp; $line2 = $_; my $PERcode = (split(/,/,$line2))[0]; my $FDXcode = (split(/,/,$line2))[1]; my $QUANTITY1 = (split(/,/,$line2))[2]; my $QSIGN1 = (split(/,/,$line2))[3]; my $AMT1 = (split(/,/,$line2))[4]; my $ASIGN = (split(/,/,$line2))[5]; my $SECTYPE1 = (split(/,/,$line2))[6]; if (($CODE eq $PERcode) && ($QSIGN eq $QSIGN1) && ($AMTSIG eq $ASIG +N) && ($SECTYPE eq $SECTYPE1)){ $TRNCODE = $FDXcode; } } } close inData2; } This is where I am at, but In many it does not matter what each field +is so I do not want to compare fields if in the table the field is bl +ank. in this example it would return TRN = RCV and DLV, which is not what I + want to do. I would want to return TRN = DLV because the other field +s in the table are blank if ($CODE1 eq "$field1") { $TRN = "$field2"; } Thanks in advace,

Replies are listed 'Best First'.
Re: Building If Statements
by NetWallah (Canon) on Aug 06, 2009 at 19:57 UTC
    It sounds like you are building a Finite State Machine.

    For a small number of states and rules, you could probably build you own dispatch table.

    If it gets beyond 7 or 8 rules, I'd recommend looking at DFA::Simple.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Building If Statements
by jethro (Monsignor) on Aug 06, 2009 at 19:31 UTC
    my $PERcode = (split(/,/,$line2))[0]; my $FDXcode = (split(/,/,$line2))[1]; ...

    is better written as

    my ($PERcode,$FDXcode,...)= split /,/,$line2;

    Apart from that I really have a hard time understanding what you want to do, especially since you put everything in code tags. For example, what is this:

    if field1 ne "" field7 ne ""

    Is this the code your are trying to parse? Because it doesn't look like perl

    Would it work for you if you wrote just this:

    if ($CODE1 eq $PERcode) && ($QSIGN1 eq '' or $QSIGN eq $QSIGN1) && ($A +SIGN eq '' or $AMTSIG eq $ASIGN) ...

    This ignores any empty fields and just compares the fields that are set

    UPDATED