Closed Thread
Results 1 to 5 of 5

Thread: Left Fill in Perl?

  1. #1
    Cosmet is offline Learning Programmer
    Join Date
    Oct 2006
    Posts
    58
    Rep Power
    0

    Left Fill in Perl?

    How can I pad left in perl? Basically if I have a string like

    108

    I need to fill 4 spaces in front of it with 0's. IE

    0000108

    Is this possible in Perl?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest
    Here is some code I used before:

    Function:

    Code:
    # Pad-Left 0's Function
    # Takes two parameters - Variable and number of 0s
    sub zero_fill
    {
        $pad_len = $_[1] - length($_[0]);
        $padded = sprintf("%0${pad_len}d", $_[0]);
        print "$padded\r\n";
        return $padded
    }

    Call it like:

    Code:
    zero_fill($text,13)
    Where $text is what you want to be padded left and 13 is the amount of 0's you want it to pad left by.

  4. #3
    Cosmet is offline Learning Programmer
    Join Date
    Oct 2006
    Posts
    58
    Rep Power
    0
    I'm a little confused. Do I put the function at the top and then:

    Code:
    zero_fill("108",4")
    ?

  5. #4
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    You put the function wheverever you want to in your script. It really does not matter.

  6. #5
    Cosmet is offline Learning Programmer
    Join Date
    Oct 2006
    Posts
    58
    Rep Power
    0
    Cool, got it working right after I made that last post and never responded back. Thanks for your help.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How fill like spiral?
    By qwertyuiop123 in forum PHP Development
    Replies: 2
    Last Post: 06-23-2011, 02:49 PM
  2. PDF Form Fill - with PHP
    By shackrock in forum PHP Development
    Replies: 0
    Last Post: 12-17-2010, 01:27 PM
  3. Please help to fill out my questionnaire
    By chow74528 in forum C and C++
    Replies: 5
    Last Post: 03-20-2010, 08:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts