On 4/20/05, Keith Worthington <[EMAIL PROTECTED]> wrote:
> 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
> 

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 = split(/(')|("))/, $v_dim1);
my ($v_feet_str, $v_inch_str) = $split[1] eq "'" ? ($split[0],
$split[3]) : (0, $split[0]);

HTH,

--jay

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to