castle has asked for the wisdom of the Perl Monks concerning the following question:
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
Now I'm reading the above 2 files in my code as follows.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>
#! /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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: perl if condition
by GrandFather (Saint) on Feb 27, 2010 at 13:29 UTC | |
Re: perl if condition
by pajout (Curate) on Feb 27, 2010 at 11:02 UTC |
Back to
Seekers of Perl Wisdom