Here is some code that makes use of a couple of very nice perl features.

1) You can put your data after the _END_ statement and then have while read it in, rather than putting it in an array at the source. Makes for easier setting of test cases.

2) It uses REGEX power of Perl to split the line. This is much more flexible than split, and is the usual way that perl programmers parse stuff.

I have added a couple of lines to your original input to show that a) if it is not REGION but say HQ the value is not replaced. Ditto if it is REGION but the original value is not XYZ. Not sure if that is exactly what you meant but the concept should be useful.

#!/usr/bin/perl -w while (<DATA>) { #read a line into $_ chomp; $_ =~ m/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/; # col1 col_b xyz Descr my ($col1, $data_b, $xyz, $description ) = ($1, $2, $3, $4); if ( ( $description =~ m/Region/ ) && ( $xyz eq "XYZ" ) ) { $xyz = + "N/A"; } print "Line $. = $_\n"; print "\tXYZ result = $xyz \n"; } #end while DATA exit 1; __END__ ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription at RowCode ValuesInColumn1 DataColumnB ABC RowDescription at RowCode ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::HQ ValuesInColumn1 DataColumnB BCD RowDescription|RowCode|Suppli +er ID::Region

The result of running the above is:

Line 1 = ValuesInColumn1 DataColumnB XYZ RowDescription|RowCo +de|Supplier ID::Region XYZ result = N/A Line 2 = ValuesInColumn1 DataColumnB XYZ RowDescription|RowCo +de|Supplier ID::Region XYZ result = N/A Line 3 = ValuesInColumn1 DataColumnB XYZ RowDescription|RowCo +de|Supplier ID::Region XYZ result = N/A Line 4 = ValuesInColumn1 DataColumnB XYZ RowDescription at Ro +wCode XYZ result = XYZ Line 5 = ValuesInColumn1 DataColumnB ABC RowDescription at Ro +wCode XYZ result = ABC Line 6 = ValuesInColumn1 DataColumnB XYZ RowDescription|RowCo +de|Supplier ID::HQ XYZ result = XYZ Line 7 = ValuesInColumn1 DataColumnB BCD RowDescription|RowCo +de|Supplier ID::Region XYZ result = BCD

In reply to Re: Split lines in file to columns by pgmer6809
in thread Split lines in file to columns by Magnolia25

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, details, 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, summary, 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.