0

I have an array of strings that represent sizes.

A list of all format variations is below:

  1. 2x3
  2. 3.6x5.6
  3. 6'RD

Goal: Convert to these formats:

  1. 2' x 3'
  2. 3' 6'' x 5' 6''
  3. 6' ROUND

All values are currently being pushed into an array called @sizearray like so:

push(@sizearray, $data[$size_num]);

Then,

my @formattedsize = @sizearray;

foreach (@formattedsize) {

    if ($formattedsize[$_] =~ /(\d+)x(\d+)/) {

        #convert to

    if (???) {

        #???

    }

    if (???) {

        #???

    }

}

How would I go through each element in the array and save the values into a new array with the new format?

5
  • You are trying to solve 2 problems: 1) parse the input to extract "meaningful" data, i.e. geometry (rectangular, round, etc) and parameters (aspect ratio, diameter, etc). Before you can do that you must establish the "universe" of possibilities. Are there more than just rectangular and round? This is the harder part. (2) take the extracted data and normalize/standardize the format. This is the easy part. Commented Jul 21, 2013 at 17:36
  • You do realise there are 12 inches in a foot, not 10? Commented Jul 21, 2013 at 17:36
  • @DavidKnipe, I am trying to convert a spreadsheet file from my supplier's format into the format I need for my database. For some reason, they wrote sizes in this way. But, although it may seem otherwise, the decimal is referring to inches and not a base10 decimal. Commented Jul 21, 2013 at 17:41
  • @JimGarrison, I have listed the "universe of possibilities." There is only the versions that you see. For example, there are no entries like 2.3x5. If there are inches, there are exactly six inches and for both the width and the height. Commented Jul 21, 2013 at 17:42
  • @JimGarrison, "The easy part" is where I am failing to understand exactly how this would be done. Commented Jul 21, 2013 at 17:42

1 Answer 1

1

You are trying to solve 2 problems:

  1. Parse the input to extract "meaningful" data, i.e. geometry (rectangular, round, etc) and parameters (aspect ratio, diameter, etc). Before you can do that you must establish the "universe" of possibilities. Are there more than just rectangular and round? This is the harder part.
  2. take the extracted data and normalize/standardize the format. This is the easy part

Let's say you only have two options, rectangular and round. Rectangular seems to be defined by a pair of real numbers separated by an 'x', so a regex for that might be

(\d+(?:\.\d+)?)\s*x\s*(\d+(?:\.\d+)?)

What you have here is two expressions for real numbers:

  • 1 or more digits followed by an optional group of a dot and one or more digits
  • optional whitespace, an x and more optional whitespace
  • 1 or more digits followed by an optional group of a dot and one or more digits

The outer parentheses around the number expression is a capturing group that causes the regex engine to make whatever matched available in the results. The inner parentheses (?:\.\d+)? is a non-capturing group (the ?: part). It allows you to apply the trailing ? quantifier (0 or 1) to the decimal portion but not capture it separately.

If the input doesn't match this, you move on to the next pattern, looking for a round specification. Repeat as needed for all possibilities.

For the above expression

# assume string to be parsed is in $_
if (my ($h,$w) = /(\d+(?:\.\d+)?)\s*x\s*(\d+(?:\.\d+)?)/))
{
    printf "%s x %s\n", $h, $w;
}

I haven't tested this so there may be a typo... but this is the general idea.

Sign up to request clarification or add additional context in comments.

7 Comments

Say this is contained in an if statement as written above in the question, and say this correctly results true for only the formats it was meant to, how would I then convert this into the desired format?
Answer updated. Beyond this you'll have to some of your own work. Try it out in the Perl command-line debugger (perl -de0). You can enter expressions and program lines one at a time and see them evaluated.
Just, if you could answer one last question. I am receiving a warning that "Argument "8x11" isn't numeric in array element..." Why would that matter?
In which statement do you see that? Sounds like a different part of the program to me.
This is a separate problem. $formattedsize[$_] is an array reference, so the thing inside [ ] must be a number. At that point in your code, $_ already contains the input string, so you just want $_=~/(\d+)x(\d+)/).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.