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.
mmcwAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ozoConnect With a Mentor Commented:
#if you're assigning instead of printing
$count_ID = sprintf "%0${use_count_ID_width}d\n",$count_ID;
#also, the $ is outside the {}, not inside
# ${use_count_ID_width}d not ${$use_count_ID_width}d
0
 
freesourceCommented:
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"
0
 
ozoCommented:
$\="\n"; for( "0001".."0004" ){ print }
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
mmcwAuthor Commented:
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

0
 
ozoCommented:
for( 1,2,10,101,245,1234 ){
    printf "%0${countnumber}d\n",$_;
}
0
 
mmcwAuthor Commented:
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";

0
 
mmcwAuthor Commented:
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!!
0
 
freesourceCommented:
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.
0
All Courses

From novice to tech pro — start learning today.