in reply to More elegant way than multiple "if"?
Something like this?
use strict; use warnings; my @data = split /\n/, <<'DATA'; 324K 324K 440K 533K 23T 224G 42G 42G 1.9T 709G 294K 294K 684K 684K 492K 492K 62M 64M 48K 41M 34M 433K 317K 812K DATA my %units = ( K => 1024**1, M => 1024**2, G => 1024**3, T => 1024**4, ); my $unit_re = join '|', sort keys %units; $unit_re = qr/$unit_re/; my $number_re = qr/[0-9]+(?:\.?[0-9]+)/; foreach (@data) { warn "Line seems malformed: $_" && next unless /^\s*($number_re)($unit_re)\s+($number_re)($unit_re)\s* +$/; my $diff = $3 * $units{$4} - $1 * $units{$2}; print "Diff: $diff\n"; }
|
|---|