Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
CX89 Apple tree
CX89 Sailing boat
CX89 Apple tree
AC75 Apple candy
CX89 Sailing boat
#!/usr/bin/perl -- use strict; use warnings; use diagnostics; use Text::CSV; use Text::CSV_XS qw( ); #main { use autodie 2.06; open( my $fh_in, '<', 'data.txt' ); my $col_rules = [ [ [ 1 => eq => 'Apple'], [ 0 => eq => 'CX89'], [ "%s %s tree\n" => [ 0, 1 ] ], ], [ [ 1 => eq => 'Sailing'], [ 0 => eq => 'CX89'], [ "%s %s boat\n" => [ 0, 1 ] ], ], [ [ 0 => ne => 'CX89'], [ 1 => eq => 'Apple'], [ 1 => ne => 'Sailing'], [ "%s %s candy\n" => [ 0, 1 ] ], ], ]; process_rules( $col_rules, $fh_in, \*STDOUT ); } sub process_rules { my ( $rules, $in, $out ) = @_; my $csv_in = Text::CSV->new( { sep_char => ',', eol => $/ } ) or die Text::CSV->error_diag(); my $csv_out = Text::CSV->new( { eol => $/ } ) or die Text::CSV->error_diag(); while ( my $row = $csv_in->getline($in) ) { my $rulematch = 0; for my $ruleset (@$rules) { last if $rulematch; my $ruleset_match = 0; for my $rule ( @$ruleset[0 .. $#$ruleset ] ) { my ( $ix, $op, $val ) = @$rule; $ruleset_match++ if $op eq 'eq' and $val eq $row->[$ix]; $ruleset_match++ if $op eq 'ne' and $val ne $row->[$ix]; } #~ warn '$rulematch ',$rulematch; #~ warn '$ruleset_match ', $ruleset_match ; #~ warn '$#$ruleset ', $#$ruleset ; if ( $ruleset_match == $#$ruleset ) { $rulematch++; my ( $printf, $indices ) = @{ $ruleset->[-1] }; $out->printf( $printf, (@$row)[@$indices] ); } } } } __DATA__ "CX89",Apple "CX89",Sailing "CX89",Apple "AC75",Apple "CX89",Sailing

In reply to Re: parsing CSV with TEXT::CSV by Anonymous Monk
in thread parsing CSV with TEXT::CSV by GertMT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found