in reply to process multiline text and print in desired format
This is an example of a problem that formats can solve quite nicely:
#!/usr/bin/perl use strict; use warnings; our %rec; # formats need global variables, not mere lexicals format STDOUT_TOP = Product Product basic all overse +as Release Type color colors shippi +ng ---------------------------------------------------------------------- +-------- . ; format STDOUT = ^>>>>>>>>>> ^<<<<<<<<<<< ^<<<<<<<<<< ^<<<<<<<<<<<<<<<<<< ^<< +<<<<< ~~ $rec{rel}, $rec{type}, $rec{bascol}, $rec{allcol}, $rec +{ovship} . ; # to read a file instead, use this and change "<DATA>" below to "<IN> +" # open IN, '<', $ARGV[0] or die "open $ARGV[0]: $!"; $: .= ','; # split filled lines also on comma while (<DATA>) { chomp; if (m/^Release Date([\d\/]+$)/) { %rec = (type => 'none', map { $_ => 'N/A' } qw(bascol allcol ovship)); $rec{rel} = $1; } $rec{type} = $1 if m/^\s+product [^(]+\(([^)]+)\)$/; $rec{bascol} = $1 if m/^\s+color basic (.*)$/; $rec{allcol} = $1 if m/^\s+color all (.*)$/; $rec{ovship} = $1 if m/^\s+overseas shipping (.*)$/; write if m/^$/; } write # emit the last record if there was no trailing blank line __DATA__ Release Date2/2/2019 product clock1(analog) color basic white color all white,black,silver warranty 1 year not sold yet Release Date2/2/2020 product none Release Date2/2/2021 product clock1(digital) color basic black color all black,silver warranty 1 year not sold yet Release Date2/2/2022 product clock2(digital) color basic white color all white overseas shipping yes shipping charges yes warranty 1 year not sold yet
See perlform for more information about the format mechanism, although it is somewhat obscure and most suited to simple scripts like this. If this is part of a larger system as some of my fellow monks suspect, this is probably a sub-optimal solution.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: process multiline text and print in desired format
by ak_mmx (Novice) on Mar 18, 2021 at 01:49 UTC | |
by jcb (Parson) on Mar 18, 2021 at 22:14 UTC |