Hi All,
Here is my code so far. I am really getting frustrated with my inability to
get this right.
I didn't understand Chris' earlier suggestion about using defined but I tried
using it anyway.
I cannot seem to get the pattern match to properly handle a dimension that is
just feet or just inches.
I would really appreciate some pointers on this problem.
#!/usr/bin/env perl
use strict;
use warnings;
open(INFILE, "size_input.txt") or die "Can't open input.txt: $!";
while (<INFILE>) { # assigns each line in turn to $_
my $v_border_id = "";
my $v_size = "";
my $v_dim1_str = "";
my $v_dim2_str = "";
my $v_tag = "";
# Echo out the input line.
print "\nInput line:\n $_";
# Perform a case insensitive check for the proper data format. Capture the
# desired parts of the data using parentheses.
if (/.*border:\s*(.*)\s*size:\s*(.*)\s*tag:\s*(.*)\s*/i){
# Store the capture patterns in variables to avoid unpredictable results.
my ($v_border_str, $v_size_str, $v_tag_str) = ($1, $2, $3);
# Check for no border.
if ($v_border_str =~ /none/i){
$v_border_id = "";
} else {
$v_border_id = $v_border_str;
}
# Parse up the size string.
if ($v_size_str =~ /\d+\s*['"]\s*x\s*\d+\s*['"]/i){
# It looks like a size string so continue to process.
$v_size = "matched size pattern";
# Split the size string into its two parts.
($v_dim1_str, $v_dim2_str) = split(/\s*x\s*/i, $v_size_str);
# Now split dimension one into feet and inch parts.
#my ($v_feet_str, $v_inch_str) = split(/(?:'|")/, $v_dim1_str);
$v_dim1_str =~ /\s*([\d\.\s\-\/]*)\s*'??\s*([\d\.\s\-\/]*)\s*"??/;
#my ($v_feet_str, $v_inch_str) = ($1, $2);
my $v_feet_str = defined $1 ? $1 : 0;
my $v_inch_str = defined $2 ? $2 : 0;
print "Dimension 1\n";
print " Feet: $v_feet_str\n";
print " Inches: $v_inch_str\n";
# Check for fraction in inch string.
if ($v_inch_str =~ /\//){
# There is a fraction to deal with.
# Parse the fraction using whitespace or a hyphen (-) and the
# forward slash (/) character.
my ($v_whole_in, $v_numer, $v_denom) = split(/(\s+|-|\/)/,
$v_inch_str);
print "Whole inches: $v_whole_in\n";
print "Numerator: $v_numer\n";
print "Denominator: $v_denom\n";
}
# Merge the components of the dimension into a single value.
# $v_dim1_str_ft * 12 + $v_dim1_str_in
# Parse up the second dimension.
# Merge the components of the dimension into a single value.
# Assign the smaller dimension to width.
# Assign the larger dimension to length.
} else {
$v_size = "didnt match pattern";
}
# Check for no tag.
if ($v_tag_str =~ /none/i){
$v_tag = "";
} else {
$v_tag = $v_tag_str;
}
} else {
#print "bad format\n";
$v_border_id = "";
$v_size = "";
$v_tag = "";
}
print "Size string: $v_size\n";
print "Dimension 1 string: $v_dim1_str\n";
print "Dimension 2 string: $v_dim2_str\n";
}
close INFILE;
# cat size_input.txt
Border: None Size: 9' x 25' Tag: None
Border: None Size: 9' x 30' Tag: None
Border: None Size: 9' x 20' Tag: None
Border: None Size: 6'1" x 12'7" Tag: None
Border: None Size: 7'6" x 12'7" Tag: None
Border: None Size: 7'11" x 12'7" Tag: None
Border: None Size: 12'7" x 14'1" Tag: None
Border: None Size: 4'11" x 6'1" Tag: None
Border: None Size: 7'10" x 16' Tag: None
Border: None Size: 83' X 40" Tag: None
Border: None Size: 17' x 50' Tag: None
Border: None Size: 5' X 90'6" Tag: None
Border: None Size: 39" X 100" Tag: None
Border: None Size: 30" x 12' Tag: None
Border: None Size: 28-3/8" x 14'4" Tag: None
Border: None Size: 16' 6-3/4" x 43" Tag: None
Border: None Size: 21'3 1/2" x 24' Tag: None
Border: None Size: 14'8.5" x 16'7" Tag: None
Kind Regards,
Keith
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>