Hi.
I think you have two separate problems here, right?
Gufranul Haque wrote:
> Hello all,
>
> I have a series of numbers 20.45,-2.00,45.450,-30.390
>
> 20.45 - length is 5
> -2.00 - length is 4
>
> I need to calculate the length of each number for formatting purposes.
FIrst you need to decide how many decimal places you're interested in.
Since you've used both 2 and 3 places here so I'll go for 3. To find the
length of the largest number, use sprintf like this:
my $n1 = -30.390;
my $large = sprintf "%0.3f", $n1;
The zero will make the string as short as possible, but long enough for
the number. Now format your smaller number to match the length:
my $n2 = $n1 * 0.1;
my $small = sprintf "%*.3f", length $large, $n2;
The * will make sprintf take the field length from the parameter
list, where we use the length of the other number. Now print them
out:
print $large, "\n";
print $small, "\n";
output:
-30.390
-3.039
I think this is what you want.
> I am trying to split the each number into an array of charcters and
> then calculating the length of the array
>
> my @digits = split(/\S/, @number[$i])
> my @length = @digits
I've just clicked what you're doing here! The problem is already
solved, above, but to split a string into an array of characters,
split on the null string:
my @digits = split '', $number[$i];
now get the length of the array by
my $length = @digits;
you were simply copying your @digits to another array, @length.
> but somehow it doesn't seem to be working. Can someone tell me the
> corect expression to use.
Bt you can easily find the length of a string with 'length' as I did
above.
Hope this helps,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]