Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

perl if condition

by castle (Initiate)
on Feb 27, 2010 at 10:49 UTC ( [id://825656]=perlquestion: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re: perl if condition
by GrandFather (Saint) on Feb 27, 2010 at 13:29 UTC

    First off, a few things you need to do always in your code:

    • use strictures (use strict; use warnings; - see The strictures, according to Seuss)
    • eschew goto
    • keep the scope of variables as small as possible
    • use the three parameter version of open and use lexical file handles
    • check that file operations succeeded and report errors if they didn't
    • use consistent indentation

    Then there are a few things that will help your PerlMonks nodes:

    • read the helpful hints below the text edit area
    • ensure the preview looks like you expected it to and fix your node if it doesn't
    • avoid long lines with no white space - they cause nasty stuff to happen in some browsers
    • use the minimum amount of data required to show the issue - you may have to fake some up
    • show what you get and what you expected
    • describe what is wrong with what you got and how it is different than what you expected
    • avoid using external files if possible. Use __DATA__ or strings as files. Use STDOUT for output
    • provide runable code that doesn't depend on anything else (as far as possible)
    • spend at least as long crafting your node as you expect other to spend solving your problem

    Ok, now that's out of the way. Here is what may constitute a solution to your problem:

    use strict; use warnings; my $conditions_txt_file = <<FILESTR; 1 eq 1720 5 eq R FILESTR my $test_txt_file = <<FILESTR; 0,1720,123,123,13,123 0,1720,123,123,13,R 0,465,123,123,13,123 FILESTR my @conditions; open my $c_card, '<', \$conditions_txt_file or die "Can't open conditi +ons file: $!\n"; while (<$c_card>) { chomp; next if ! length; push @conditions, [split ' ']; } close $c_card; open my $testHandle, '<', \$test_txt_file or die "Can't open data nfil +e: $!\n"; while (<$testHandle>) { chomp; next if ! length; my @columns = split ','; next if grep {! match ($_, @columns)} @conditions; print join (',', @columns), "\n"; } close $testHandle; sub match { my ($cond, @columns) = @_; my ($column, $test, $value) = @$cond; if ($test eq 'eq') { return if $column < 0 || $column >= @columns; my $match = $columns[$column] eq $value; return $match; } else { die "Don't know how to perform '$test' test\n"; } }

    Prints:

    0,1720,123,123,13,R

    The 'tricky' matching stuff is tucked away in a subroutine that is called within a grep that applies each test to the line being checked and returns a count of failed tests. If none failed they must all have succeeded and the line gets printed. If the number of conditions is huge there may be an advantage in using an explicit loop rather than using grep, but for most purposes grep is likely to be much easier to understand and plenty fast enough.


    True laziness is hard work
Re: perl if condition
by pajout (Curate) on Feb 27, 2010 at 11:02 UTC
    Hello,
    please, read Writeup Formatting tips. In my opinion, it is better to invest 10 minutes to format question properly than invest 10 minutes to decode badly formatted one...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://825656]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found