Put list of colours in __DATA__ where it can easily be changed. Get the name of the phone file from the command line. Extract the model from each record of the file with a regular expression. (My regexp assumes that the 'columns' are seperated by TABs.) The processing is the same as before.
use strict;
use warnings;
use Readonly;
# Assume input file name on command line (default is 'phones.txt).
Readonly::Scalar my $PHONE_FILE => $ARGV[0] || 'phones.txt';
Readonly::Array my @COLOURS => <DATA>;
# Assume columns seperated by tabs, all other whitespace is significa
+nt.
Readonly::Scalar my $GET_MODEL => qr{\A ( [^\t]+ ) }x;
open my $PHONES, '<', $PHONE_FILE or die "Cannot open $PHONE_FILE\n";
while (my ($phn) = <$PHONES> =~ $GET_MODEL) {
foreach my $colr (@COLOURS) {
print "\n$phn $colr";
}
print "\n===";
}
close $PHONES
__DATA__
baby blue
baby pink
black
dark blue
brown
dark purple
green
orange
hot pink
light purple
red
white
yellow
Note: Readonly is not necessarey. Its purpose here is to assure the reader that those symbols are logically constants.
|