Hi, I have a file "conditions_file.txt" which has the following details. The details here form an if condition in my code. 1st column is position, 2nd column is the operator, 3rd column is the value to be checked)


1 eq 1720
59 eq R

I've my input file "test.txt" having the following lines

0,1720,123,123,13,123,123,123,12,3,123,13,123,123,123,123,123,123,13,1 +23,12,312,312,32,3,213,123,213,23,123,123,123,12,312,3,123,123,123,21 +3,12,31,231,23,13,123,123,12,312,3,123,123,1,23,123,12,3,123,12,3,R,4 +23,234,23,423,4<br> 0,1720,123,123,13,123,123,123,12,3,123,13,123,123,123,123,123,123,13,1 +23,12,312,312,32,3,213,123,213,23,123,123,123,12,312,3,123,123,123,21 +3,12,31,231,23,13,123,123,12,312,3,123,123,1,23,123,12,3,123,12,3,Q,4 +23,234,23,423,4<br> 0,465,123,123,13,123,123,123,12,3,123,13,123,123,123,123,123,123,13,12 +3,12,312,312,32,3,213,123,213,23,123,123,123,12,312,3,123,123,123,213 +,12,31,231,23,13,123,123,12,312,3,123,123,1,23,123,12,3,123,12,3,P,42 +3,234,23,423,4<br>
Now I'm reading the above 2 files in my code as follows.
#! /usr/bin/perl my $line1; my @condition; my %conditions_hash; my @conditions_keyvalues; my $line2; my @test_array; $flag = 0; $line2 = $ARGV[0]; open(OUTPUT_HANDLE,">output.txt"); open(TEST_HANDLE,"test.txt"); open(C_CARD,"conditions_file.txt"); while($line1 = <C_CARD>) { @condition = split(' ',$line1); $hash_condition{$condition[0]} = "$condition[1]"." $condition[2]"; push(@conditions_keyvalues,"$hash_condition{$condition[0]}\n"); $flag = 1 if($condition[1] =~ m/eq/); goto l1; } #print "$hash_condition{$condition[0]}\n"; l1: open(OUTPUT_HANDLE,">>output.txt"); while($line2 = <TEST_HANDLE>) { chomp($line2); @test_array = split(',',$line2); if($flag == 1) { #print "$test_array[$condition[0]]\n"; #print "$condition[2]\n"; foreach my $i(@conditions_keyvalues) { print "key values are $i\n"; if($test_array[$condition[i]] eq $condition[i+2]) { print OUTPUT_HANDLE "$line2\n"; } } } }

I read first line from conditions_file.txt and check in my test.txt to see if the second value in the csv file is 1720. This works. But I've to recursively check all the conditions in the conditions_file.txt. If any line from the test.txt file match all the conditions, I must write that line to the output.txt file.
Please help me.


In reply to perl if condition by castle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.