Professional Web Applications Themes

fixed width fields - PERL Miscellaneous

This should be an easy one but I'm having a brain cramp. I have a string: 2-s1.0-49FR-WSJ0-TWC1-X-12345-00. I want to replace the 12345 with $i. When $i equals "1" how can I make it "00001"?...

  1. #1

    Default fixed width fields

    This should be an easy one but I'm having a brain cramp.

    I have a string: 2-s1.0-49FR-WSJ0-TWC1-X-12345-00. I want to
    replace the 12345 with $i. When $i equals "1" how can I make it
    "00001"?
    Jim Guest

  2. #2

    Default Re: fixed width fields

    Jim wrote:
     

    perldoc -f sprintf

    example:

    $a="2-s1.0-49FR-WSJ0-TWC1-X-12345-00";
    $i=234;

    $b=sprintf("%05i",$i);
    $a=~s/-\d{5}-/-$b-/;

    print $a,"\n"

    peter


    --
    peter pilsl
    at
    http://www.goldfisch.at

    peter Guest

  3. #3

    Default Re: fixed width fields

    Jim <com> wrote:
     

    $str =~ s/12345/sprintf "%05d", $i/e;

    --
    Vlad
    Vlad Guest

  4. #4

    Default Re: fixed width fields

    Jim wrote:

    (snipped)
     


    #!perl

    $string = "2-s1.0-49FR-WSJ0-TWC1-X-12345-00";
    $i = 1;

    if ($i == 1)
    { substr ($string, -8, 5, "00001"); }

    print $string;


    PRINTED RESULTS:
    ________________

    2-s1.0-49FR-WSJ0-TWC1-X-00001-00


    Purl Gurl
    --
    Wildest Chatline Ever Written
    http://www.purlgurl.net/~callgirl/webchat/chahta.cgi
    Purl Guest

  5. #5

    Default Re: fixed width fields

    Vlad Tepes wrote:
     
     [/ref]
     


    "When $i equals "1" how can I make it "00001"?"


    You will note the originating author specifies a parameter
    of $i being equal to one, and no other value.


    Purl Gurl
    --

    #!perl

    $string = "2-s1.0-49FR-WSJ0-TWC1-X-12345-00";

    $i = 0;

    $string =~ s/12345/sprintf "%05d", $i/e;


    print $string;


    PRINTED RESULTS:
    ________________

    2-s1.0-49FR-WSJ0-TWC1-X-00000-00
    Purl Guest

  6. #6

    Default Re: fixed width fields

    Jim (com) wrote on MMMDCLXXIV September MCMXCIII in
    <URL:news:google.com>:
    ~~ This should be an easy one but I'm having a brain cramp.
    ~~
    ~~ I have a string: 2-s1.0-49FR-WSJ0-TWC1-X-12345-00. I want to
    ~~ replace the 12345 with $i. When $i equals "1" how can I make it
    ~~ "00001"?


    $i = 1;
    $str = "2-s1.0-49FR-WSJ0-TWC1-X-12345-00";
    substr $str => 27, 5 => sprintf "%05d" => $i;


    Abigail
    --
    BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
    {["", "Just ", "another ", "Perl ", "Hacker\n"] -> [shift]};
    $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
    print 1, 2, 3, 4;
    Abigail Guest

  7. #7

    Default Re: fixed width fields

    [This followup was posted to comp.lang.perl.misc]

    In article <google.com>, jg_x30
    iwon.com says... 

    $data = sprintf "%05d",$i;
    Barry Guest

Similar Threads

  1. fixed width variable
    By Jeff Tillotson in forum PERL Beginners
    Replies: 5
    Last Post: December 2nd, 04:17 PM
  2. Replies: 0
    Last Post: November 21st, 04:58 PM
  3. Unload a fixed width file
    By Jonathan Leffler in forum Informix
    Replies: 2
    Last Post: September 8th, 09:01 AM
  4. Fixed Width layout cells expanding with text ??
    By DittoMary webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 24th, 10:20 PM
  5. Replies: 0
    Last Post: January 8th, 02:59 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139