Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
<p>Hi, I have a file "conditions_file.txt" which has the following det +ails. 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) </p> <br>1 eq 1720 <br>59 eq R <p>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> </p> 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"; } } } } <p> 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'v +e 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. </p>

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (3)
    As of 2020-05-25 07:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (144 votes). Check out past polls.

      Notices?