Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: perl if condition

by GrandFather (Saint)
on Feb 27, 2010 at 13:29 UTC ( [id://825667]=note: print w/replies, xml ) Need Help??


in reply to perl if condition

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found