Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: perl if condition by GrandFather
in thread 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 having an uproarious good time at the Monastery: (2)
    As of 2020-06-05 00:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?



      Results (35 votes). Check out past polls.

      Notices?