Home > Archive > PERL Beginners > April 2005 > More help with split
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 |
More help with split
|
|
| Keith Worthington 2005-04-20, 3:56 pm |
| Hi All,
I continue to work on my first perl program. :-) I am stuck again and could
use your guidance.
Here is the code snippet that I am working on right now. The problem that I
have is that when processing a size like 30" x 150'6" it sees the first
dimension as a number of feet. How can I get perl to recognize that something
like this is implicitly 0' 30"?
# Parse up the size string.
if ($v_size_str =~ /[0-9]*['"][ ]*x[ ]*[0-9]*['"]/i){
# It looks like a size string so continue to process.
# Split the size string into its two parts.
($v_dim1, $v_dim2) = split(/[ ]*x[ ]*/i, $v_size_str);
# Now split dimension one into feet and inch parts.
my ($v_feet_str, $v_inch_str) = split(/(?:'|")/, $v_dim1);
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(/(?: |-|\/)/,
$v_inch_str);
print "Whole inches: $v_whole_in\n";
print "Numerator: $v_numer\n";
print "Denominator: $v_denom\n";
}
Kind Regards,
Keith
| |
| Jay Savage 2005-04-20, 3:56 pm |
| On 4/20/05, Keith Worthington <keithw@narrowpathinc.com> wrote:
> Hi All,
>=20
> I continue to work on my first perl program. :-) I am stuck again and co=
uld
> use your guidance.
>=20
> Here is the code snippet that I am working on right now. The problem tha=
t I
> have is that when processing a size like 30" x 150'6" it sees the first
> dimension as a number of feet. How can I get perl to recognize that some=
thing
> like this is implicitly 0' 30"?
>=20
> # Parse up the size string.
> if ($v_size_str =3D~ /[0-9]*['"][ ]*x[ ]*[0-9]*['"]/i){
> # It looks like a size string so continue to process.
> # Split the size string into its two parts.
> ($v_dim1, $v_dim2) =3D split(/[ ]*x[ ]*/i, $v_size_str);
> # Now split dimension one into feet and inch parts.
> my ($v_feet_str, $v_inch_str) =3D split(/(?:'|")/, $v_dim1);
> print "Feet: $v_feet_str\n";
> print "Inches: $v_inch_str\n";
> # Check for fraction in inch string.
> if ($v_inch_str =3D~ /\//){
> # 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) =3D split(/(?: |-|\/)/,
> $v_inch_str);
> print "Whole inches: $v_whole_in\n";
> print "Numerator: $v_numer\n";
> print "Denominator: $v_denom\n";
> }
>=20
> Kind Regards,
> Keith
>=20
Keith,
In this case, you need to keep the seperator. split will do this if
you use parentheses. It also returns and extra "undef" delimiter in
this case, so you want to watch out for that. take a look at the
conditional operator in perldoc perlop to fins out what all is going
on here.
my @split =3D split(/(')|("))/, $v_dim1);
my ($v_feet_str, $v_inch_str) =3D $split[1] eq "'" ? ($split[0],
$split[3]) : (0, $split[0]);
HTH,
--jay
|
|
|
|
|