Link to home
Start Free TrialLog in
Avatar of mmcw
mmcw

asked on

Update to $x numbers

Hello,

Is it possible to do the following.

I have a value:

$countnumber = "4";

I have a number that will automaticaly be made by a little counter.
$test = 1;
The value $test will be increased:

1 becomes 2
2 becomes 3
3 becomes 4.

What I now want is that the output
will not be show as 1 or 2 but as
0001 or 0002 (4 number like the value I gave in $countnumber.
Avatar of freesource
freesource

Try this from the command line.

perl -e '$test = 1; $countnumber =4; while ($test <= $countnumber) { printf "000%d\n", $test; $test++; }'

Read about printf and sprintf in "perldoc perlfunc"
Avatar of ozo
$\="\n"; for( "0001".."0004" ){ print }
Avatar of mmcw

ASKER

Sorry,

I think I did no explain my problem OK.

I have a lot of numbers like:

1
2
10
101
245
1234

I want them al to look the same!!

I want them all to look like this:

0001
0002
0010
0101
0245
1234

The $countnumber variable tells me how to format the numbers.

When $countnumber = "4" the numbers have to be formated like above.

When I set $countnumber = "8";
the numbers have to be formated like:

00000001
00000002
00000010
00000101
00000245
00001234
 
Is this possible!!

greetinsg Michel

for( 1,2,10,101,245,1234 ){
    printf "%0${countnumber}d\n",$_;
}
Avatar of mmcw

ASKER

I have tried this. Both will not work!!

$count_ID = "10";
$use_count_ID_width = "4"

$count_ID = printf "%0${$use_count_ID_width}d\n",$count_ID;
      

<input name="$_" type="text" value='printf "%0${$use_count_ID_width}d\n",$count_ID' size="60%" size="40">

The result has to be:

$count_ID = "0010";

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmcw

ASKER

Sorry freesource,

I have to rejects your answer!!

The answer OZO gave was what I wanted!
OZO please answer again so you will given the points to!!
Your forgiven Michael, I certainly gave the correct answer for the way your original question was worded.  It appeared you only wanted to go to 4.