Question: I'm searching a way to reduce the following piece of code to a single regexp statement:

```
if( $current_value =~ /(\d+)(MB)*/ ){
        $current_value = $1 * 1024 * 1024;
    }
    elsif( $current_value =~ /(\d+)(GB)*/ ){
        $current_value = $1 * 1024 * 1024 * 1024;
    }
    elsif( $current_value =~ /(\d+)(KB)*/ ){
        $current_value = $1 * 1024;
    }

```

The code performs an evaluation of the value that can be expressed as a single number (bytes), a number and KB (kilobytes), with megabytes (MB) and so on. Any idea on how to reduce the block code?

Answer: 
You could set up a hash like this:

```
my %FACTORS = ( 'KB' => 1024, 'MB' => 1024**2, 'GB' => 1024**3 );

```

And then parse the text like this:

```
if ( $current_value =~ /(\d+)(KB|MB|GB)/ ) {
    $current_value = $1 * $FACTORS{$2};
}

```

In your example the regex has a `*` which I'm not sure you intend, because `*` means "zero or more" and so `(+\d)(MB)*` would match `10` or `10MB` or `10MBMB` or `10MBMBMBMBMBMBMB`.
You can do it in **one** regexp, by putting code snippits *inside* the regexp to handle the three cases differently

```
my $r;

$current_value =~ s/
    (\d+)(?:
          Ki (?{ $r = $^N * 1024 })
        | Mi (?{ $r = $^N * 1024 * 1024 })
        | Gi (?{ $r = $^N * 1024 * 1024 * 1024 })
    )/$r/xso;

```