Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 663923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:32:33+00:00 2026-05-13T23:32:33+00:00

I want to use Perl’s sprintf to zerofill a variable. sprintf(%08d, $var); But I

  • 0

I want to use Perl’s sprintf to zerofill a variable.

sprintf("%08d", $var);

But I want to dynamically determine how many digits to zerofill.

How do I replace the “8” in sprintf("%08d", $var) with a variable called $zerofill?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:32:33+00:00Added an answer on May 13, 2026 at 11:32 pm

    The first argument to sprintf is just a string:

     my $zerofill = 9;
     my $number = 1000;
     my $filled = sprintf "%0${zerofill}d", $number;
    

    Notice the braces to set apart the variable name from the rest of the string.

    We have this particular problem as a slightly clever exercise in Learning Perl to remind people that strings are just strings. 🙂

    However, as mobrule points out in his answer, sprintf has many features to give you this sort of flexibility. The documentation for such a seemingly simple function is quite long and there are a lot of goodies in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a proxy with this perl script but I'm not sure
I want to use Perl to extract information from a Certificate Signing Request ,
I want to use regular expressions (Perl compatible) to be able to find a
I don't want to use sendmail to send an email but would prefer to
I want to use some perl line, like this: perl -pe $_=~s///e The problem
I use a Perl one-liner to create an SQL statement, but I am not
I have dozens of perl script on linux server, and I want use some
I'm trying to use the bignum module in Perl and want to set the
I want to use the perl to check the hard disk space on Windows
I am new to perl and I want to use screen input on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.