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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
ozoCommented:
#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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.