Home > Archive > PERL Beginners > April 2005 > Need help match feet and inches
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Need help match feet and inches
|
|
| Keith Worthington 2005-04-20, 8:56 pm |
| 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
| |
| Paul Kraus 2005-04-20, 8:56 pm |
| Hello,
Please stop making new threads for the same topic. Reply to the same
thread. Keeps the group organized as well as our inboxes and archive
searches.
If someone else who had a simliar issue and did a google search they
could click one link and see all of your efforts. By splitting them up
it others loose the ability to learn from your work.
Not trying to be a pain.
Paul
| |
| Keith Worthington 2005-04-20, 8:56 pm |
| On Wed, 20 Apr 2005 15:34:37 -0400, Paul Kraus wrote
> Hello,
> Please stop making new threads for the same topic. Reply to the same
> thread. Keeps the group organized as well as our inboxes and archive
> searches.
> If someone else who had a simliar issue and did a google search they
> could click one link and see all of your efforts. By splitting them
> up it others loose the ability to learn from your work.
>
> Not trying to be a pain.
> Paul
>
My apologies to all.
I had thought that as I progressed the problems should be addressed seperately.
Kind Regards,
Keith
| |
| Chris Devers 2005-04-20, 8:56 pm |
| On Wed, 20 Apr 2005, Keith Worthington wrote:
> I had thought that as I progressed the problems should be addressed
> seperately.
If the problems are isolated, sure, that makes sense.
In this case though, you're exploring the evolution of one umbrella
issue, so it makes sense to keep things consolidated :-)
--
Chris Devers
| |
| Offer Kaye 2005-04-20, 8:56 pm |
| On 4/20/05, Keith Worthington wrote:
> Hi All,
>=20
> Here is my code so far. I am really getting frustrated with my inability=
to
> get this right.
>=20
> I didn't understand Chris' earlier suggestion about using defined but I t=
ried
> using it anyway.
>=20
> I cannot seem to get the pattern match to properly handle a dimension tha=
t is
> just feet or just inches.
>=20
> I would really appreciate some pointers on this problem.
>=20
Hi Keith,
I assumed from your question that your main problem now is in handling
the size part ( what you called $v_size_str) and splitting it up into
4 parts - a first dimension feet and inches and a second dimension
feet and inches.
So here is some code to help you out. It doesn't break any of the
parts into sub-parts (e.g. 28-3/8 into the 3 numbers) but I assume you
can do that yourself. It also assumes you already have a variable
holding just the size part. For convenience (mine :-)), the code reads
from __DATA__ and assigns to $_ instead of $v_size_str, but you
shouldn't have any trouble modifing it. Here's the code:
############# begin code
use strict;
use warnings;
while (<DATA> ) {
chomp;
my ($dim1,$dim2) =3D split /\s*x\s*/i;
print "=3D=3D$dim1=3D=3D =3D=3D$dim2=3D=3D\n";
my ($dim1_feet, $dim1_inches) =3D get_sub_dims($dim1);
my ($dim2_feet, $dim2_inches) =3D get_sub_dims($dim2);
print "The sub-dims are: $dim1_feet, $dim1_inches, $dim2_feet,
$dim2_inches\n";
}
sub get_sub_dims {
my $dim =3D shift;
my ($feet, $inches) =3D (0,0);
if ($dim =3D~ m/^\s*(.+?)'/) {
$feet =3D $1;
$dim =3D~ s/^\s*(.+?)'\s*//;
}
if ($dim =3D~ m/^\s*(.+?)"/) {
$inches =3D $1;
}
return ($feet, $inches);
}
__DATA__
9' x 25'
7'6" x 12'7"
7'10" x 16'
83' X 40"
17' x 50'
5' X 90'6"
39" X 100"
30" x 12'
28-3/8" x 14'4"
16' 6-3/4" x 43"
21'3 1/2" x 24'
14'8.5" x 16'7"
############# end code
If anything in the code isn't clear, please don't hesitate to ask.
HTH,
--=20
Offer Kaye
| |
| John W. Krahn 2005-04-20, 8:56 pm |
| Keith Worthington wrote:
> Hi All,
Hello,
> 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.
Did you see the code I posted earlier?
John
--
use Perl;
program
fulfillment
| |
| Keith Worthington 2005-04-21, 3:56 pm |
| On Wed, 20 Apr 2005 15:19:28 -0700, John W. Krahn wrote
> Keith Worthington wrote:
>
> Hello,
>
>
> Did you see the code I posted earlier?
>
> John
John,
For some reason I didn't get your post until after I sent out my other
desperate plea for help.
Your post gave me the following two critical REs.
(?:(\d+)')?
(?:(\d+)")?
Which I have since modified to
/(?:([\d\.\s\-\/]+)')?\s*(?:([\d\.\s\-\/]+)")?/
I creep ever closer to a solution. :-)
I will be posting against the original thread from now on per the list's
gentle admonition. ;-)
Kind Regards,
Keith
|
|
|
|
|