Jeremy
Jeremy

Reputation: 821

Missing zero in hash of array

Does anyone know why the 0 in "1.30" doesn't show up?

Code:

#!/usr/bin/perl 

our %mb_version = (
'TXA4' => [1.30, 1.23],
);

foreach (@{$mb_version{'TXA4'}}) {
  print "$_\n";
}

Output:

1.3
1.23

Upvotes: 4

Views: 96

Answers (3)

James Green
James Green

Reputation: 1753

I'd avoid the answers suggesting formatting to two decimal places explicitly, and prefer storing version identifiers as strings, which is what they really want to be. E.g.

our %mb_version = (
    'TXA4' => [qw/1.30, 1.23/],
);

Upvotes: 0

depsai
depsai

Reputation: 415

Same way you can try this also....

foreach (@{$mb_version{'TXA4'}}) {
  sprintf("%02f", $_);
}

Upvotes: 1

perreal
perreal

Reputation: 97948

If you are sure that the version number has 2 decimal places you can do:

foreach (@{$mb_version{'TXA4'}}) {
  printf "%.2f\n", $_;
}

otherwise, you can use strings (not floats) to store version numbers.

Upvotes: 3

Related Questions