in reply to Search between pattern and append
Update: I looked back upon this thread and these 2 lines could have been better written. I don't remember why I did what I did in the original coding. I do remember that I wrote the code in an attempt to get the OP "unstuck" and was surprised at the "one liner" requirement. Anyway the OP obviously doesn't care about this, but other future readers might...However, I will add that in this sort of situation, the difference probably doesn't matter at all...#!/usr/bin/perl use strict; use warnings; while (<DATA>) { process_record ($1) if /^\s*START\s*(\w+)/; } sub process_record { my $start_value = shift; my @record; my $line; while (defined ($line=<DATA>) and $line !~ /^\s*END/) { push @record, $line; } my ($end_value) = $line =~ /^\s*END\s+(\w+)/; @record = map{"$start_value $end_value $_"}@record; print @record; print "\n"; } =prints abc xyz MAY abc xyz JUNE abc xyz JULY oue 2345 MAY oue 2345 JUNE oue 2345 JULY =cut __DATA__ START abc MAY JUNE JULY END xyz blah ....more blah... START oue MAY JUNE JULY END 2345
# @record = map{"$start_value $end_value $_"}@record; # print @record; print "$start_value $end_value $_" for @record;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Search between pattern and append
by Anonymous Monk on Jul 13, 2018 at 17:59 UTC | |
by Marshall (Canon) on Jul 13, 2018 at 18:13 UTC | |
by Anonymous Monk on Jul 13, 2018 at 18:29 UTC | |
by Marshall (Canon) on Jul 13, 2018 at 18:57 UTC |