G'day ak_mmx,
Welcome to the Monastery.
The following script shows how I'd probably attack this problem.
See the notes at the end for some details.
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
my $data = 'pm_11129830_input.txt';
my $fmt = "%-24s%-15s%-14s%-24s%s\n";
my @headings = (
'Product Release',
'product Type',
'color basic',
'color all',
'overseas shipping',
);
printf $fmt, @headings;
{
open my $fh, '<', $data;
local $/ = '';
while (<$fh>) {
my ($rel, $type, $colb, $cola, $ship)
= ('', 'none', 'N/A', 'N/A', 'none');
for my $item (map /^\s*(.+)$/, split /\n/) {
for ($item) {
/^Release Date/ && do {
$rel = $item;
last;
};
/^product [^(]+\(([^()]+)/ && do {
$type = $1;
last;
};
/^color basic (.+)$/ && do {
$colb = $1;
last;
};
/^color all (.+)$/ && do {
$cola = $1;
last;
};
/^shipping charges (.+)$/ && do {
$ship = $1;
last;
};
}
}
printf $fmt, $rel, $type, $colb, $cola, $ship;
}
}
Output:
Product Release product Type color basic color all
+ overseas shipping
Release Date2/2/2019 analog white white,black,silve
+r none
Release Date2/2/2020 none N/A N/A
+ none
Release Date2/2/2021 digital black black,silver
+ none
Release Date2/2/2022 digital white white
+ yes
Notes:
-
Use a lexical filehandle in as small a scope as possible; that's my $fh in the code above.
Using a package variable (e.g. INPUT, OUTPUT, etc.) may work fine in a short script like you posted;
however, that looks to me like a proof-of-concept script and, when put into more substantial code,
there could easily be conflicts with other variables using the same name.
Also note that Perl will close $fh for you when it goes out of scope.
-
Use the 3-argument form of open.
This is the preferred method and there are a number of reasons for doing this.
See the linked documentation for more about that;
you may also find perlopentut useful.
-
Hand-crafting I/O error messages is tedious and error-prone.
Your message actually has a problem in that it doesn't report the reason for the failure
(e.g. file doesn't exist; you don't have read permission; something else).
Using the autodie pragma is much easier:
it saves you work and gives you better feedback if something goes wrong.
-
The 'local $/ = ''; line specifies reading in paragraph mode.
See "perlvar: $/" for more about that.
-
The 'for ($item) {...}' code is effectively a switch/case construct.
I find this easy to write, read and, where necessary, modify.
There are other methods for achieving this:
see "perlsyn: Basic BLOCKs";
be wary of those flagged as experimental and I'd strongly advise that you don't use those in production code.
-
When you want to lay things out in columns, printf can make this very easy
(sprintf has substantial documentation on the formats available).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.