From: Aruna Goke <[EMAIL PROTECTED]> > I have worked on the code below and am able to generate the 15digit > lenght required . > > However, what i wish to achieve is to make sure all the output comes out > in 15 digits each. a sample of my output is as below. > > Can someone guide on how to make all come out in 15digits. > > Thank you. > > Goksie > > #!/usr/bin/perl > > use warnings; > use strict; > use Perl6::Say; > > # make a subrouting to generate random numbers; > my ($ab, $ac, $ad, $a, $b, $c, $d); > sub pin_number($$$){ > ($b, $c, $d) = @_; > for($a=0; $a<=10; $a++){ > $ab = int(rand($b)); > $ac = int(rand($c)); > $ad = int(rand($d)); > say $ab, $ac, $ad;
1. Don't say you want them output, printf() them in the requested format printf "%05d%05d%05d\n", $ab, $ac, $ad; 2. int(rand(9)); will give you a random integer between 0 and 8 (inclusive)! Not one between 0 and 9! You need to use int(rand(10)); for that. 3. If you want to repeat something 11 times (or did you actually mean 10 times?) you can write it like this: for (0..10) { ... } 4. $a and $b should be used only within the sort {block}. They are not checked by use strict. 5. $a, $b, $c, $d, $ab, $ac, $ad ?!? This is not a programmable calculator, this is a programming language, you are not restricted to two character variable names. Use something that actually has a meaning. 6. If you declare all your variables on top of the script, they are about as global as if you dropped use strict and did not declare them at all. Declare variables within the block in which you need them, never before! 7. sub pin_number ($$$) ? Don't use subroutine prototypes. They are NOT what you seem to think they are. Believe me, it's better to keep away until you really really know you do need them. Prototypes are NOT a way to specify the number of expected parameters, they are a way to instruct the parser to parse something differently than it normaly would. While at ocassions it may look like it's the same thing, it's not. Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/