http://qs321.pair.com?node_id=1231629


in reply to Split lines in file to columns

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