in reply to Format of number by template

From what I understand of your intentions, you want to:
  1. Pad a number (with zeroes) out to a given number of digits (printf and its sister sprintf are useful for this)
  2. Break the padded number up into sections (substr is made for slicing up strings)
  3. Reformat these sections according to your "template" (printf calls it a "format")

Here's one way to do it, step-by-step

#!/usr/bin/perl use warnings; use strict; my $num = 34; #example number my $padded_num = sprintf("%015d",$num); #Step 1: Pad the number my $first = substr($padded_num, 0, 3); #Step 2: Break the padded numb +er up my $second = substr($padded_num, 3, 5); my $third = substr($padded_num, 8, 7); my $string = sprintf("NO-%s-%s-%s", $first, $second, $third); #Step 3: + Reformat the sections print $string . "\n";

davis
Is this going out live?
No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

Replies are listed 'Best First'.
Re: Re: Format of number by template
by nite_man (Deacon) on Feb 12, 2003 at 13:50 UTC
    Davis, thanks for your reply. Your way isn't suit for me because template isn't fixed. I can have any numbers of templates and they can be different.
    So, it's hard to define format for sprintf.
    Michael